Hi all,
I have the following format for date in a table 20161031 (numeric).
Which is the best way to create the last 1 (20161024), 2 (20161017) weeks in the same format so as to put them in a macro element?
I am trying to perform some computations between the given periods but i would like to establish a more dynamic procedure for future use.
Thank you in advance
A
If your date variable is a number like 20161031 then you need to convert it
into a sas date as in next demo
data _null_;
dtn = 20161031;
date _var = input(dtn,yymmdd8.);
run;
In case the date variable is a sas date, then just subtrcat 7 from the date to get the previous same day of week:
data _null_;
date_var = '31OCT2016'd;
date_1 = date_var - 7;
date_2 = date_var - 14;
put date_var ddmmyy10. date_1 ddmmyy10. date_2 ddmmyy10. ;
run;
If your date variable is a number like 20161031 then you need to convert it
into a sas date as in next demo
data _null_;
dtn = 20161031;
date _var = input(dtn,yymmdd8.);
run;
In case the date variable is a sas date, then just subtrcat 7 from the date to get the previous same day of week:
data _null_;
date_var = '31OCT2016'd;
date_1 = date_var - 7;
date_2 = date_var - 14;
put date_var ddmmyy10. date_1 ddmmyy10. date_2 ddmmyy10. ;
run;
If you are going to do any manipulation of dates, intervals between two dates or increment dates then you want to put the values into SAS date valued variables so the functions INTNX and INTCK are available. @Shmuel shows how. You really do not want to attempt to manipulate date values in the macro language as the syntax gets very ugly, hard to read and fragile. Use a datastep and then assign the value of the dates to macro variables using SYMPUTX.
Thank you very much for your replies, i usually incorporate dates in datetime into macros and using INTCK and INTNX functions but these type of date in numeric form gave me trouble because after put in a macro it wasnt beeing resolved
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.