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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
LinusH
Tourmaline | Level 20

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
Cboath
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

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 ;

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
  • 5 replies
  • 970 views
  • 1 like
  • 3 in conversation