DATA Step, Macro, Functions and more

macro variables for day, month and year

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

macro variables for day, month and year

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


Accepted Solutions
Solution
‎03-04-2018 08:58 PM
Super User
Posts: 13,084

Re: macro variables for day, month and year

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;

View solution in original post


All Replies
Moderator
Posts: 311

Re: macro variables for day, month and year

[ Edited ]

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.  ;
Frequent Contributor
Posts: 91

Re: macro variables for day, month and year

Posted in reply to AndrewHowell

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.

Moderator
Posts: 311

Re: macro variables for day, month and year

(See above)

Solution
‎03-04-2018 08:58 PM
Super User
Posts: 13,084

Re: macro variables for day, month and year

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;
Moderator
Posts: 311

Re: macro variables for day, month and year

@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?

Frequent Contributor
Posts: 91

Re: macro variables for day, month and year

This was the perfect solution, thank you!

Super User
Super User
Posts: 7,860

Re: macro variables for day, month and year

[ Edited ]

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.));

 

Super User
Posts: 9,612

Re: macro variables for day, month and year

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 91

Re: macro variables for day, month and year

Posted in reply to KurtBremser
Thank you
Moderator
Posts: 311

Re: macro variables for day, month and year

Elliott, once you're done, can you please select the solution you found most helpful & flag it as "Accepted as solution"? Thanks.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 234 views
  • 2 likes
  • 5 in conversation