BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
desas
Calcite | Level 5

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 

 

desas_0-1711981360221.png

 

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 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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. 

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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. 

 

desas
Calcite | Level 5
Thank you so much !!
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 297 views
  • 0 likes
  • 3 in conversation