BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Elliott
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

10 REPLIES 10
AndrewHowell
Moderator

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.  ;
Elliott
Obsidian | Level 7

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.

ballardw
Super User

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;
AndrewHowell
Moderator

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

Elliott
Obsidian | Level 7

This was the perfect solution, thank you!

Tom
Super User Tom
Super User

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

 

Kurt_Bremser
Super User

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.

Elliott
Obsidian | Level 7
Thank you
AndrewHowell
Moderator
Elliott, once you're done, can you please select the solution you found most helpful & flag it as "Accepted as solution"? Thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 14924 views
  • 2 likes
  • 5 in conversation