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

Hi there,

I want to create certain number of macro variables within a macro. Those macro variables are used as input variables in a macro in a unique parameter. For example:

%macro example (vars);

    data t;

         input mylib.mydata (keep = &vars);

run;

 %mend;

%example (vars= a b c);

 

Since the number of variables I pass into the macro each time can be different, I want to assign separate values to each one (e.g. a, b, and c) to perform certain calculations. I create a temporary dataset z in the macro as follow:

Var    value

a         20

b         17

c        30

I’m not sure how to create three (or #) macro variables from the previous output. I’m trying something like shown below, but it is not working:

   data _null_;

                 set z;

                   %do i=1 to &numb;        /*previously determined in the macro (in this case 3)*/

                                 %if _n_ = i  %then  call symputx  ( “value&i” , value);

                   %end;

              run;

Could you please advice?

Thanks a lot,

AG.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @alexgonzalez 

Could you please specify the expected result?

Do you mean that you want to create a macrovariable "a" that has the value 20, etc. ?

In this case, no need to have a do loop:

data _null_;
	set z;
    call symputx (var, value);
    /*or*/
    call symputx ("value"||left(_n_),value);
run;

Log:

 87          %put &a. &b. &c.;
 20 17 30
 88          %put &value1. &value2. &value3.;
 20 17 30

View solution in original post

8 REPLIES 8
ed_sas_member
Meteorite | Level 14

Hi @alexgonzalez 

Could you please specify the expected result?

Do you mean that you want to create a macrovariable "a" that has the value 20, etc. ?

In this case, no need to have a do loop:

data _null_;
	set z;
    call symputx (var, value);
    /*or*/
    call symputx ("value"||left(_n_),value);
run;

Log:

 87          %put &a. &b. &c.;
 20 17 30
 88          %put &value1. &value2. &value3.;
 20 17 30
alexgonzalez
Quartz | Level 8

That's exactly what I wanted. Assign values to each one of the variables.

Thank you!

ed_sas_member
Meteorite | Level 14
You're welcome!
Quentin
Super User

Hi,

 

You don't need a macro loop to create multiple macro variables with CALL SYMPUTX.  Since the DATA step is a loop, who can use that, e.g.:

 

data lookup ;
  input var $1. value ;
  cards ;
a 20
b 17
c 30
;

data _null_ ;
  set lookup ;
  call symputx(cats("Value",_n_),value) ;
run ;

%put &=Value1 &=Value2 &=Value3 ;

Will create the three macro variable value1-value3:

400  %put &=Value1 &=Value2 &=Value3 ;
VALUE1=20 VALUE2=17 VALUE3=30

And there are other ways to build a similar "array" of macro variables.  

 

That said, I think your idea of creating a dataset Z with name-value pairs was a good one.  Instead of reading that dataset to create macro variables and using those macro variables to pass values to your macro, it might be cleaner to simply pass name of the dataset Z to your macro.  That often allows you to avoid the mess of going from values stored in a SAS dataset, to values stored as text in macro variables, and then back to values stored in SAS datasets.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
alexgonzalez
Quartz | Level 8

Perfect, thank you very much.

Tom
Super User Tom
Super User

I am not understanding your question at all. Your example macro has ONE parameter. It accepts a list of VARIABLE names. How do macro variables come into the problem?  Are they the input or the output? Where?

 

Are you saying you want to create macro variables that have the same names as the actual variables? Just use CALL SYMPUTX(). You can force the macro variables into the global symbol table using the third parameter.

 

If the variables are all of the same type you can use an array.

array x &vars;
do i=1 to dim(x);
  call symputx(vname(x[i]),x[i],'g');
end;

If not then you need to make a macro loop:

%do i=1 %to %sysfunc(countw(&vars),%str( ));
call symputx("%scan(&var,&i,%str( ))",%scan(&var,&i,%str( )),'g');
%end;

 

alexgonzalez
Quartz | Level 8

Sorry if I was not clear enough for you. I already got some replies with the solution. Very much appreciated your input.

Jagadishkatam
Amethyst | Level 16

please try

 

data z;
input Var$ value;
cards;
a 20
b 17
c 30
;


   data _null_;
                 set z;
                   do i=1 to 3;        /*previously determined in the macro (in this case 3)*/
                                 if _n_ = i  then  call symputx  (var , value);
                   end;
              run;


%put &a &b &c;
Thanks,
Jag

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 625 views
  • 3 likes
  • 5 in conversation