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

Hi!

Ive come across a issue that I cant seem to find the answer to. Any recommenations most appreciated.

I would like create a  series of global variable from inside a macro, the variable name needs to be change according to its contents. 

However I can not use call symput as this is only used in datastep, and I am unaware how(if) a %let command can have dynamicf (changing) variable name.

Kind Regards, Tekla

Example

%macro create_string(inf, varlist);    *loopar infile * frågor;


%let w = %str();

     %do p= 1 %to %sysfunc(countw(&inf.));

                %do i= 1 %to %sysfunc(countw(&varlist.));   

                       %let a = %scan(&inf.,&p.);

                       %let r = %sysfunc(cats(%scan(&inf.,&p.),%scan(&varlist.,&i.)));

                       %let v = &v.&r.hh;

                %end;

%let w = %sysfunc(tranwrd(&v.,hh,%str()));

call symput('x'||&a.,&w.);   * <-- here is the  problem - only works in data step - not inside a  macro!;

%let w = %str();

%end;

%mend;

%create_string(&alla_in., &allv.);

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

%global variablename; Inside the macro before the first reference to the variable is a good idea.

View solution in original post

4 REPLIES 4
ballardw
Super User

%global variablename; Inside the macro before the first reference to the variable is a good idea.

data_null__
Jade | Level 19

you could write

%global x&a;

%let x&a = &w;

in place of

call symput('x'||&a.,&w.);


However I think you can simplify this a good deal.  Usually CAT family of function are not need in macro language and there is no need to count words.  What is this program suppose to do.  Show example input and output desired.

ndp
Quartz | Level 8 ndp
Quartz | Level 8

%macro create_string(inf, varlist);    *loopar infile * frågor;

%let w = %str();

     %do p= 1 %to %sysfunc(countw(&inf.));

                 %let a = %scan(&inf.,&p.); /* moved outside of loop for i */

                %do i= 1 %to %sysfunc(countw(&varlist.));   

                        %let r = &a.%scan(&varlist.,&i.);

                       %let v = &v.&r.hh; /***v is not defined earlier; why add hh if removed latter for w*/

                %end;

%let w = %sysfunc(tranwrd(&v.,hh,%str()));

%global x&a; /* define global macro variable */

%let x&a. = &w.;   /* problem solved */

%let w = %str();

%end;

%mend;

%create_string(&alla_in., &allv.);

Tom
Super User Tom
Super User

Hard to tell what you want.  Looks like you want one variable for each value in the first argument and the value is a concatenation of each of the words in the second list.

But it is really not clear to me what separator you wanted between the generated words, as I could not figure out the purpose of "hh", so I just used space as the delimiter.


So if I call it with

%create_string(a b,x y z);

Then it will create XA = ax ay az and XB=bx by bz. 


%macro create_string(inf, varlist);  

%local p i a w sep ;

%do p=1 %to %sysfunc(countw(&inf.));

   %let a = %scan(&inf.,&p.);

   %global X&a ;

   %let w= ;

   %let sep= ;

   %do i=1 %to %sysfunc(countw(&varlist.));  

      %let w=&w.&sep.&a.%scan(&varlist.,&i.);

      %let sep=%str( );

   %end;

   %let X&a = %unquote(&w) ;

%end;

%mend create_string;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 6830 views
  • 0 likes
  • 5 in conversation