DATA Step, Macro, Functions and more

Error when trying to return a value from SAS Macro Function after using a data step.

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Error when trying to return a value from SAS Macro Function after using a data step.

Hello,

I've built a macro that will merge the values of a data set and create a new data set with an updated name. I'm trying to return the name in the macro, but it is throwing a couple errors.

The errors occur on the merge statement and on the last return statement. I'm not sure why since if I remove the return statement everything works. But, the second I add it in, these two errors reappear. Any suggestions would be appreciated.

/* Build Data */

data scores;

   input Name $ Test_1 Test_2 Test_3;

  datalines;

Bill 187 97 103

Carlos 156 76 74

Monique 99 102 129

;

data scores_post;

   input Name $ Test_1 Test_2 Test_3;

  datalines;

Bill 23 14 61

Carlos 51 23 44

Monique 13 812 43

;

%let num = 0;

%macro update_info(old,new);

%global num;

%let num = %eval(&num.+1);

%let new_name = &new._&num.;

/* Update information */

data &new_name.;

merge &old. &new.;

run;

/* Return new dataset name */

&new_name.

%mend;

%let catch = %update_info(scores,scores_post);


Accepted Solutions
Solution
‎06-12-2013 09:52 PM
Super User
Super User
Posts: 6,499

Re: Error when trying to return a value from SAS Macro Function after using a data step.

Don't bother to return the name as SAS is already doing that for you in the SYSLAST automatic macro variable.

%update_info(scores,scores_post);

%let catch=&syslast ;

View solution in original post


All Replies
Super User
Posts: 5,256

Re: Error when trying to return a value from SAS Macro Function after using a data step.

I'm not sure if you can return values in this manner, at the same time as the macro is submitting code.

Either way, why don you just declare new_name as %global?

Data never sleeps
New Contributor
Posts: 4

Re: Error when trying to return a value from SAS Macro Function after using a data step.

You are allowed to return values in this manner. Take for example this macro:

macro isWeekend(date);

    %if date=  %then %do;

        %let date = %sysfunc(today());

    %end;

    %let week_day = %sysfunc(weekday(&date.));

    %if &wd.=1 or &wd.=7 %then %do;

        %let is_it_the_weekend = 1;

    %end;

     %else;

          %let is_it_the_weekend = 0;

     %end;


    &is_it_the_weekend

%mend;


Calling would be like:

%put %isWeekend(%sysfunc(today()));

This should return either a 0 or 1 and can be used in future macros for specific calendar pulls, et cetera.

The reason why I do not want to declare a global variable is because then this code and code like this will lose the ability to be easily picked up and placed in other projects without an issue with context. The ability to return a value is critical.

Super User
Super User
Posts: 6,499

Re: Error when trying to return a value from SAS Macro Function after using a data step.

Remember that SAS macro is not a "language" it is just a macro.  That is a utility to facility the generation of text.

Your example works because you are capturing ALL of the generated text.  Other than the 0 or 1 that macro does not generate any code that macro needs to pass to SAS to execute.

Super User
Super User
Posts: 6,499

Re: Error when trying to return a value from SAS Macro Function after using a data step.

Since your macro is generating actual SAS code that you want to execute you cannot "return" a value by making it the generated code.  Instead you must return the value in a macro variable or a dataset. 

To make it flexible pass the NAME of the macro variable as a parameter.  You can specify default values for the new parameters in the macro definition if you want.  You can even create a new GLOBAL macro variable when the requested macro variable does not already exist.

%macro update_info(old,new,new_name=catch,counter=num);

%if not %symexist(&counter) %then %global &counter;

%if not %symexist(&new_name) %then %global &new_name;

%let &counter = %eval(&&&counter + 1);

%let &new_name = &new._&&&counter ;

/* Update information */

data &&&new_name ;

  merge &old &new ;

run;

%mend;

%let num = 0;

%let catch= ;

%update_info(scores,scores_post,new_name=catch,counter=num);

Solution
‎06-12-2013 09:52 PM
Super User
Super User
Posts: 6,499

Re: Error when trying to return a value from SAS Macro Function after using a data step.

Don't bother to return the name as SAS is already doing that for you in the SYSLAST automatic macro variable.

%update_info(scores,scores_post);

%let catch=&syslast ;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 240 views
  • 1 like
  • 3 in conversation