I have always used the below macro to determine the previous date and always worked :
data _null_;
year = year(input("&sysdate9",date9.));
call symput('rptdt', strip(put(date(),YYMMDDN.)-2));
call symput('time', put(time(), time5.));
call symput('year',compress(put(year,best.)));
run;
but suddenly , SAS is acting weird and I am getting dates as below : it is showing as 20240399, in 90's series
and today's date is being displayed as 20240400
Our laptops were updated to windows11 , and this started , I am not sure If I am making a mistake or is this coincidental with windows 11.
Please help
Why would
call symput('rptdt', strip(put(date(),YYMMDDN.)-2));
Have ever worked?
That would convert a date like 01APR2024 into a string like '20240401' and convert that string into the number 20,240,401 and subtract 2 to get the number 20,240,399 then convert that into the string ' 202403999' then convert that into the string '20240399' and finally assign it to a macro variable.
Are you trying to generate a digit string in YYYYMMDD style for the date that is two days before the current date?
call symputX('rptdt', put(date()-2,YYMMDDN8.));
Do you want the YEAR to reflect the date when the SAS process started (&sysdate or &sysdate9) or the when the data step ran, DATE()? Do you want the TIME to reflect when the SAS process started (&systime) or when the data step ran?
There can be a big difference. See this example where the date that is 2 days before NOW is actually after the date when I started my SAS session.
130 data _null_;
131 now=datetime();
132 call symputX('rptdt', put(datepart(now)-2,YYMMDDN8.));
133 call symputX('time', put(timepart(now),tod5.));
134 call symputX('year', year(datepart(now)));
135 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
136 %put &=rptdt &=time &=year &=sysdate9 &=systime ;
RPTDT=20240330 TIME=10:40 YEAR=2024 SYSDATE9=29MAR2024 SYSTIME=23:06
PS Never use the ancient CALL SYMPUT() function unless the goal is to generate a macro variable with leading and/or trailing spaces.
Why would
call symput('rptdt', strip(put(date(),YYMMDDN.)-2));
Have ever worked?
That would convert a date like 01APR2024 into a string like '20240401' and convert that string into the number 20,240,401 and subtract 2 to get the number 20,240,399 then convert that into the string ' 202403999' then convert that into the string '20240399' and finally assign it to a macro variable.
Are you trying to generate a digit string in YYYYMMDD style for the date that is two days before the current date?
call symputX('rptdt', put(date()-2,YYMMDDN8.));
Do you want the YEAR to reflect the date when the SAS process started (&sysdate or &sysdate9) or the when the data step ran, DATE()? Do you want the TIME to reflect when the SAS process started (&systime) or when the data step ran?
There can be a big difference. See this example where the date that is 2 days before NOW is actually after the date when I started my SAS session.
130 data _null_;
131 now=datetime();
132 call symputX('rptdt', put(datepart(now)-2,YYMMDDN8.));
133 call symputX('time', put(timepart(now),tod5.));
134 call symputX('year', year(datepart(now)));
135 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
136 %put &=rptdt &=time &=year &=sysdate9 &=systime ;
RPTDT=20240330 TIME=10:40 YEAR=2024 SYSDATE9=29MAR2024 SYSTIME=23:06
PS Never use the ancient CALL SYMPUT() function unless the goal is to generate a macro variable with leading and/or trailing spaces.
Please note what makes the code from @Tom work is that he does arithmetic (subtracting 2) on UN-formatted date values. Once ALL arithmetic and logical operations are done, then and only then, should you format the results and only if you need to do that
Do not try to do logical or arithmetic operations on formatted dates. Do not try to work with formatted dates (which are character strings) to perform any operations, other than to use the formatted dates in titles, labels or file names.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.