I am trying to create macro variables as mm, dd, yy and yyyy from a today()-1 date.
I have the variables coming out correctly but when I put them in my code to assign the value there are large spaces that are causing the code to fail.
Log Results (below code) give the correct info but there are spaces I don't want and I don't know how to remove them.
This is my code.
%LET INITIAL_DT = %SYSFUNC(TODAY(),DATE9.);
%PUT &INITIAL_DT. ;
DATA _NULL_;
CALL SYMPUT ('INITIAL_DT', "&INITIAL_DT"D);
RUN;
%PUT &INITIAL_DT. ;
DATA _NULL_ ;
format
m2 2. /* mm = 01 - 12 and dd = 01 - 31 */
d2 2. /* mm = 01 - 12 and dd = 01 - 31 */
y2 2. /* yy */
y4 4. /* yyyy */
;
wod=&INITIAL_DT -1;
call symput("wod",wod);
call symput("d2",(put(day(&wod.),z02.)));
call symput("y4",YEAR(wod));
call symput("y2",put(wod,year2.));
call symput("m2",(put(month(&wod.),z02.)));
run;
%PUT &m2. &d2. &y2. &y4. ;
Log Results:
39
40 %PUT &m2. &d2. &y2. &y4. ;
02 28 18 2018
41
Thanks in advance
Elliott
Much cleaner to do it all in the datastep
data _null_; yesterday =today()-1; call symputx("INITIAL_DT",put(yesterday,date9.)); call symputx("d2",put(day(yesterday),z2.)); call symputx("m2",put(month(yesterday),z2.)); call symputx("y2",put(yesterday,year2.)); call symputx("y4",put(yesterday,year4.)); run;
There's more efficient code, but the simplest solution to your existing code is to just change all the symput() functions to symputx() which will remove any embedded leading or trailing spaces.
Alternative:
data _null_;
call symputx("INITIAL_DT",put(today()-1,date9.));
run;
%put &=Initial_DT;
%let d2=%sysfunc(day("&INITIAL_DT"d),z2.);
%let m2=%sysfunc(month("&INITIAL_DT"d),z2.);
%let y2=%sysfunc(putn("&INITIAL_DT"d,year2.));
%let y4=%sysfunc(putn("&INITIAL_DT"d,year4.));
%put &m2. &d2. &y2. &y4. ;
Thank you, I figured there was more efficient code, what would that look like? I am always looking for more efficient ways to code things like this and learn so I can become a better programmer.
Any assistance you can offer will be greatly appreciated.
Thank you.
(See above)
Much cleaner to do it all in the datastep
data _null_; yesterday =today()-1; call symputx("INITIAL_DT",put(yesterday,date9.)); call symputx("d2",put(day(yesterday),z2.)); call symputx("m2",put(month(yesterday),z2.)); call symputx("y2",put(yesterday,year2.)); call symputx("y4",put(yesterday,year4.)); run;
@ballardw - agree, but could not tell from the email where these dates needed to be calculated - in a stand-alone data step, in the middle of a PROC SQL, etc. Assuming the date value is preset earlier, I used macros which can effectively be embedded (almost) anywhere.
Another alternative is to use FCMP functions?
This was the perfect solution, thank you!
If you don't want the spaces then don't put them into the macro variables.
Note that you can use the newer CALL SYMPUTX() function and it will automatically trim spaces from the arguments.
Also don't try to refer to macro variables that have not been created yet.
I am not sure if the extra () in the function calls cause any issue or not.
call symputx("wod",wod);
call symputx("d2",put(day(wod),z2.));
call symputx("y4",put(wod,year4.);
call symputx("y2",put(wod,year2.));
call symputx("m2",put(month(wod),z2.));
In
call symput("y4",YEAR(wod));
you did not use the put() function and so forced SAS to convert numeric to character on its own, resulting in additional blanks because of the default format.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.