DATA Step, Macro, Functions and more

Save Function Result as an Macro Variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Save Function Result as an Macro Variable

Hi,

 

I need to do following and store result into a macro variable:

 

> extract month from a variable,

> add a constant

 

I have:

%let day_act = mdy(3,13,2018);

%let i = -1;

 

%let want2 = MONTH(&day_act.) + &i.; /*expect to return numeric 2*/

 

I have been googling for quite some time and tried several combinations of SYSFUNC, EVAL etc. without desired result.

 

___ BTW. I have come up with following solution:

DATA WORK.tmp;
want2 = MONTH(&day_act.) + &i.;
RUN;


DATA _NULL_;
SET WORK.tmp;
call symput('want2', want2);
RUN;
%put &want2.; /*It gives exactly what I want*/

 

 

BUT! Oddly, when trying to do this:

 

DATA WORK.rep_&want2.;
SET WORK.something;
RUN;

 

I receive bunch of blank spaces like 

WORK.rep_           2, which obviously gives me an error.

 

 

I would very much appreciate any advice how to reach my goal without DATA step and also I am curious what causes the later error with blank spaces.

 

Thank you.


Accepted Solutions
Solution
‎04-27-2018 09:23 AM
PROC Star
Posts: 635

Re: Save Function Result as an Macro Variable

Posted in reply to JamesBlack

You need to use %SYSFUNC() in day_act too.

 

%let day_act = %SYSFUNC(mdy(3,13,2018));
%put &day_act;
%let i = -1;
%put &i;
%let want2 = %EVAL(%SYSFUNC(MONTH(&day_act.)) + &i.); /*expect to return numeric 2*/
%put &want2;

Also you can try something like this:

%let want2 =%SYSFUNC(MONTH(%SYSFUNC(INTNX(MONTH,&day_act.,-1)))); /*expect to return numeric 2*/
Thanks,
Suryakiran

View solution in original post


All Replies
Super User
Posts: 5,922

Re: Save Function Result as an Macro Variable

Posted in reply to JamesBlack

Your assignment should throgh a message about converting to char or something.

you should use put(month(),best.), and use the left() to remove preceding blanks..

left(put(month(&day_act. + &i.),best.)

 

Data never sleeps
Super User
Super User
Posts: 8,289

Re: Save Function Result as an Macro Variable

Posted in reply to JamesBlack

Unless you really want to store leading and/or trailing spaces into your macro variables you should NOT be using the older CALL SYMPUT() function.

 

Use the newer CALL SYMPUTX() which will automatically strip the leading and trailing spaces.

Occasional Contributor
Posts: 8

Re: Save Function Result as an Macro Variable

Thank you, guys.

 

Your suggestions are useful in combination with my DATA step solution, which is good.

 

I was expecting something simpler, though. When I run:

 

%let want2 = left(put(month(&day_act.) + &i.,best.));
%put &want2.;

 

I get the string 'left(put(month(mdy(3,13,2018)) + -1,best.))'. It's also useful in my code, but this time I would like to have '2' returned. I tried RESOLVE etc. If there were something like that, it would make the code much simpler.

Super User
Posts: 13,950

Re: Save Function Result as an Macro Variable

Posted in reply to JamesBlack

@JamesBlack wrote:

Thank you, guys.

 

Your suggestions are useful in combination with my DATA step solution, which is good.

 

I was expecting something simpler, though. When I run:

 

%let want2 = left(put(month(&day_act.) + &i.,best.));
%put &want2.;

 

I get the string 'left(put(month(mdy(3,13,2018)) + -1,best.))'. It's also useful in my code, but this time I would like to have '2' returned. I tried RESOLVE etc. If there were something like that, it would make the code much simpler.


If you want to actually execute a data step function such as Month in a macro assignment every single function used has to be enclosed in a %sysfunc() call. %SYSFUNC macro function tells the SAS compiler to use the data step function. Otherwise you get text.

Also the macro language doesn't "like" the put statement and wants Putn(variable, format.)

 

%let want2 = %sysfunc( left(%sysfunc (putn( %sysfunc(month(&day_act.)  ) + &i.,best.)) ) );

might work. Multiple function calls in statement gets very ugly quickly. Often I may be easier to use a data _null_ step and call symputx.

 

 

Note that there are a number of data step function that have macro equivalents such as Index/%index, Length/%Length, Scan /%scan

Solution
‎04-27-2018 09:23 AM
PROC Star
Posts: 635

Re: Save Function Result as an Macro Variable

Posted in reply to JamesBlack

You need to use %SYSFUNC() in day_act too.

 

%let day_act = %SYSFUNC(mdy(3,13,2018));
%put &day_act;
%let i = -1;
%put &i;
%let want2 = %EVAL(%SYSFUNC(MONTH(&day_act.)) + &i.); /*expect to return numeric 2*/
%put &want2;

Also you can try something like this:

%let want2 =%SYSFUNC(MONTH(%SYSFUNC(INTNX(MONTH,&day_act.,-1)))); /*expect to return numeric 2*/
Thanks,
Suryakiran
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 179 views
  • 2 likes
  • 5 in conversation