You never got June in your month because you set it once from &sysdate, but did not manipulate it the way you did with &day2.
And you have lots of confusion regarding day, day2, month, month2, year, year2.
Basically, that code is a total abomination. Discard it completely and rewrite it from scratch, according to what you need in the end.
So, to get year, month and day, I'd do this:
data _null_;
todays_date = today() - 5;
day = day(todays_date);
month = month(todays_date);
year = year(todays_date);
call symput('day',put(day,z2.));
call symput('month',put(month,z2.));
call symput('year',put(year,z4.));
run;
%put &day &month &year;
Note that you don't need the intnx function if you just want to subtract days.
What do you want as final output? The day/month/year for last month?
You're making something that is relatively easy appear to be incredibly difficult. Why not start with this:
%let cutoff_date = %eval("&sysdate9"d - 3);
If you need to (and there is no guarantee that you need to do this ... you haven't shown us what follows to extract data):
%let cutoff_date = %sysfunc(putn(&cutoff_date, date9));
You could use this instead, but it's not very useful for selecting observations in subsequent steps:
%let cutoff_date = %sysfunc(putn(&cutoff_date, yymmdd10));
You may have to explain this in words. If the program runs today, what date would you like it to return? You had hard-coded -3 in your original post.
When it comes to selecting data, you will have to show the portion of your code that uses the results to select data as well. This section just creates macro variables that are used to select data at a later (unknown to us) point.
You will have to explain a little more. For example:
"If I run the program on any day during July, I want it to return a date that is the last date in June."
If that is what you want, it's easy enough to do:
%let cutoff_date = %sysfunc(intnx(month, "&sysdate9"d, -1, e));
But you haven't yet described if that is what you need, and you absolutely need to show the later code that uses this date because the format of &CUTOFF_DATE will have to be adjusted to fit the format expected by the program.
SYSDATE is the date which your session started running. You may want to use the DATE() function to return todays date.
>Contains the date on which a SAS job or session began executing.
You never got June in your month because you set it once from &sysdate, but did not manipulate it the way you did with &day2.
And you have lots of confusion regarding day, day2, month, month2, year, year2.
Basically, that code is a total abomination. Discard it completely and rewrite it from scratch, according to what you need in the end.
So, to get year, month and day, I'd do this:
data _null_;
todays_date = today() - 5;
day = day(todays_date);
month = month(todays_date);
year = year(todays_date);
call symput('day',put(day,z2.));
call symput('month',put(month,z2.));
call symput('year',put(year,z4.));
run;
%put &day &month &year;
Note that you don't need the intnx function if you just want to subtract days.
Oh yeah, and use the {i} button to post a log. The {i} subwindow uses a fixed font and preserves all original formatting and characters.
You are calculating the MONTH and YEAR from the wrong date. Also no need for macro logic to get zero padded numbers, just use a format when you generate the macro variable.
%let mydate=%sysfunc(intnx(day,"&sysdate9"d,-3),date9);
%let day=%sysfunc(day("&mydate"d),z2);
%let month=%sysfunc(month("&mydate"d),z2);
%let year=%sysfunc(year("&mydate"d),z4);
%put &=sysdate9 &=mydate &=day &=month &=year ; SYSDATE9=02JUL2017 MYDATE=29JUN2017 DAY=29 MONTH=06 YEAR=2017
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.