Converting SAS date to month indexing system - date

How can I convert the month and year of a date to match a variable named by a month indexing system used in a study where January 1986 is month 1? I need to create a variable that calculates the difference between the current month and Jan 1986 but am not sure how to get started. My dates are currently in YYYYMMDD format.

The intck function tells you how many intervals of something occur between two dates.
monthnum = intck('month','01JAN1986'd, surveydate);
If you only have month/year, you can use the mdy function to construct a date.
dtvar = mdy(monthvar,1,yearvar);

Related

Nifi - How to add or subtract months from date?

I want to get two fields: Begin date and End date of last month. For example, 14-04-2020 should give me the Begin date as 01-03-2020 and End_date as 31-03-2020. I have read the Nifi Expression language docs but all it can do with a date format is add or subtract in milliseconds. This is not helpful for my use case as the number of days in a month is not fixed and conversion to milliseconds won't help.
Is there a way to achieve my use case somehow using Nifi Expression language?
#AdarshKumar
NiFI Expression Language for this Use Case would be very clunky and unreliable for different timezones, months with <> 30 days, and leap years.
Please reference this post below which goes into detail for how to get "last month":
How to insert previous month of data into database Nifi?
In the Case of NiFi you kind of have to play with the dates to get the outcome
if you have the dates you easily convert a date and then just hard set a day to get the 1st day of the month with a hard coded day
${now():toNumber():format('yyyy-MM-01')}
to get the last day of the month you can either use the script or play with the calc using epoch time.
so to get the last day of the previous month you can just use the date and convert the day into epoch time and subtract it from the epoch date to get last day of previous month
example
${now():toNumber():format('yyyy-MM-dd'):toDate('yyyy-MM-dd', 'GMT'):toNumber():minus(${now():toNumber():format('dd'):toNumber():multiply(86400):multiply(1000)}):format('yyyy-MM-dd')}
in this example above we convert the date to epoch format it to convert again, conversion happens to remove default timestamp and then we use the same formula to get just the day as a number to multiply it with 86400 seconds in a day and multiply that by 1000 for the epoch number to subtract from the date which is then formatted back into a date.
Raw Date: Thursday, August 26, 2021 11:20:31 AM
formatted: Thursday, August 26, 2021 12:00:00 AM
epoch of formatted date: 1629936000000
Subtract Epoch: 2246400000 (86400 seconds * 26 days * 1000)
result: 2021-07-31
alteratively you could first add a month and the work back to get the current day of the given month
this example is just to give you an idea of ways you can use built in date functions with epoch time to calculate the correct date, removing the issues with months that end on specific numbers.
I try UpdateAttribute to minus month
test${now():toNumber():format('yyyyMM'):minus(1)}01

Tableau: Same Day Last Year Auto Filter

I am trying to compare yesterday's data to the same day the year before. For example, yesterday is 11 November 2018. I want to compare to 12 November 2017 (same day but the year before). I am wanting this to be applied automatically on the filter so all I need to do is open the file and verify the numbers are correct before sending off the report.
Any help would be appreciated.
Thanks
There are many Tableau functions that manipulate dates. A couple in particular are relevant to your problem:
Today() - returns the current date
DateAdd() - adds or subtracts an interval from a date. For instance, DateAdd('year', Today(), -1) gives the date one year prior to today. The first argument to DateAdd is the level of granularity or date part.
DateDiff() - determines the difference of the interval between two dates. DateDiff('day', [Start Date], [End Date]) returns the number of days separating the two date arguments.
The functions are well documented in the online help. Construct the formulas you need and filter accordingly.
Isolate yesterday's date as its own field. For instance if that is the max date in your data, then {max([Date])} would create an LOD of the maximum date.
Then make a calculation that will display the same date last year:
year([Date]) = year([max_date])-1
and datepart('week',[Date]) = datepart('week',[max_date])
and datepart('weekday',[Date]) = datepart('weekday',[max_date])

SAS: Combine YEAR and MONTH data into a single mm/dd/yyyy date without changing types

Question: How do I combine YEAR and MONTH data into a single mm/dd/yyyy date without converting from numeric to character types?
I have date data which needs to be read into SAS. It comes in two columns, YEAR and MONTH. The data looks similar to this:
YEAR MONTH
2012 1
2012 1
2013 10
2012 2
2014 7
The data must be stored in mm/dd/yyyy format. For example, YEAR = 2013 and MONTH = 10 corresponds to 10/01/2013.
I have accomplished this via:
if month = 1 then
date = input(compress("01/01/"||year),mmddyy10.);
else if month = 2 then
date = input(compress("02/01/"||year),mmddyy10.);
...
However, the log gives the following note:
NOTE: Numeric values have been converted to character values at the
places given by:
(Line):(Column).
I understand that this is being done because SAS stores dates as numeric values since January 1, 1960. The Compress function returns a character value. Thus, the numeric data is being coerced into a character type.
While the above code sufficiently solves my problem, the note implies that date formatting should not be handled in this way (via type conversion).
Use the mdy() function:
date = mdy(month, 1, year)

How to get total experience in terms of date object

I have a condition here in which I will have total experience in terms of month and year. For example, two drop down will be there for asking total number of experience in month and year. So if I am working from 1 Jan 2012, then I will write total experience as 3 year and 11 months. Now I have to convert this 3 year and 11 months into date format so that I can save this into database
You could use java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.YEAR, year);
Date date = calendar.getTime();
As a word of caution, the day field would be set to today's date. Check the intended behaviour if the current day is outside of the bounds for the target month. For example, setting the month to February when calendar has a day field of 30. It might be wise to set the day to a known, valid value for every month (eg: 1) before setting the month and year.
Use DATE_SUB() function:
Try this:
SELECT DATE_SUB(DATE_SUB(CURRENT_DATE(), INTERVAL 3 YEAR), INTERVAL 11 MONTH);
You can use mysql's date_sub() function or <date> - interval <expression> unit syntax to subtract an interval from a date.
select date_sub(curdate(),interval '3-11' YEAR_MONTH) as start_date
UPDATE:
Following the conversation between the OP and #eggyal, the OP need to replace the period in the incoming data with - and construct an insert statement as follows:
insert into mytable (...,join_date,...) values (...,date_sub(curdate(),interval '3-11' YEAR_MONTH),...)

Zend_Date compare two dates

I have two dates given as Zend_Date(). I must check the following:
if theese dates are in the same month day (for example: second day of the month, year does not matter),
if dates are from exactly the same day(for example 2 december 2012);
To check the dates are the exact same day, you can check each individual part separately.
Zend_Date('your-date-here',Zend_Date::DAY);
This returns a date object that specifically gives you the day for the 'your-date-here' and it accepts a variety of formats; for example, I use php's date function as:
$myDate = date('Y-m-d-H-i-s');
$dateDayObject = Zend_Date($myDate, Zend_Date::DAY);
and now $dateDayObject will have the value of the 'd' in it.
You can use DAY, WEEK, YEAR... there are many constant all defined here:
http://framework.zend.com/manual/1.12/en/zend.date.constants.html
Finally, to test if they are in the same day of the month, you can use a combination of loops and if statement and the same method as above to write your own function to check the day within the month or there may be a defined constant for it. Good luck.