DATA Step, Macro, Functions and more

How to create a global variable inside a macro.

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How to create a global variable inside a macro.

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


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 10,552

Re: How to create a global variable inside a macro.

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

View solution in original post


All Replies
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 10,552

Re: How to create a global variable inside a macro.

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

Respected Advisor
Posts: 3,777

Re: How to create a global variable inside a macro.

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.

Contributor ndp
Contributor
Posts: 61

Re: How to create a global variable inside a macro.

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

Super User
Super User
Posts: 6,502

Re: How to create a global variable inside a macro.

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;

🔒 This topic is solved and locked.

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

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