BookmarkSubscribeRSS Feed
defaz
Fluorite | Level 6

hi to all,

I try to create global variables with a macro. It works flawless with this:

 

%macro init_web_parameter(varlist);
 options symbolgen;
 %put XXXXXXVARLIST: &varlist;
 %let L_cnt=1;
 %let L_var=%qscan(%quote(&varlist.),&L_cnt.,%str( ));
 %do %while(&L_var ne %nrstr( )); 
    
		%if (%symexist(&L_var.) = 0) %then %do;
			%global &L_var  ; 
			%let &L_var      = ;
                        %put XXXXXX&L_Var: # &L_var._txt # &&&L_var._txt # &sk_fg_txt #;
		%end;

    %let L_cnt=%eval(&L_cnt+1);
    %let L_var=%qscan(%quote(&varlist.),&L_cnt.,%str( ));
 %end;
 options nosymbolgen;
	
%mend;

%init_web_parameter(sk_fg sk_fg_txt)

here is the log:

 

SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg sk_fg_txt
XXXXXXVARLIST: sk_fg sk_fg_txt
SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg sk_fg_txt
SYMBOLGEN:  Macro variable L_CNT resolves to 1
SYMBOLGEN:  Macro variable L_VAR resolves to sk_fg
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
...
WARNING: Apparent symbolic reference SK_FG_TXT not resolved.
XXXXXXsk_fg: # sk_fg_txt # &sk_fg_txt # &sk_fg_txt #
                                                                                          The SAS System

SYMBOLGEN:  Macro variable L_CNT resolves to 1
SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg sk_fg_txt
SYMBOLGEN:  Macro variable L_CNT resolves to 2
...
SYMBOLGEN:  Macro variable SK_FG_TXT resolves to 
XXXXXXsk_fg_txt: # sk_fg_txt_txt # &sk_fg_txt_txt #  #

but when I try to create additional variables with a suffix, I run into problem:

 

%macro init_web_parameter(varlist);
 options symbolgen;
 %put XXXXXXVARLIST: &varlist;
 %let L_cnt=1;
 %let L_var=%qscan(%quote(&varlist.),&L_cnt.,%str( ));
 %do %while(&L_var ne %nrstr( )); 
    
		%if (%symexist(&L_var.) = 0) %then %do;
			%global &L_var  &L_var._txt  ; 
			%let &L_var      = ;
			%let &L_var._txt = ; 
                        %put XXXXXX&L_Var: # &L_var._txt # &&&L_var._txt # &sk_fg_txt #;
		%end;

    %let L_cnt=%eval(&L_cnt+1);
    %let L_var=%qscan(%quote(&varlist.),&L_cnt.,%str( ));
 %end;
 options nosymbolgen;
	
%mend;

%init_web_parameter(sk_fg); /* sk_fg_txt)    */

and again the log:

 

SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg
XXXXXXVARLIST: sk_fg
SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg
SYMBOLGEN:  Macro variable L_CNT resolves to 1
SYMBOLGEN:  Macro variable L_VAR resolves to sk_fg
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
SYMBOLGEN:  && resolves to &.
                                                                                          The SAS System

SYMBOLGEN:  Macro variable L_VAR resolves to sk_fg
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.
SYMBOLGEN:  Macro variable SK_FG_TXT resolves to 
XXXXXXsk_fg: # sk_fg_txt # &sk_fg_txt #  #
SYMBOLGEN:  Macro variable L_CNT resolves to 1
SYMBOLGEN:  Macro variable VARLIST resolves to sk_fg
SYMBOLGEN:  Macro variable L_CNT resolves to 2
SYMBOLGEN:  Macro variable L_VAR resolves to 
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted for printing.

how do I have to reference sk_fg_txt ??????

 

It oviously contains an empty string (value between the last 2 hashes), but I can't get the value by an indirect reference on the veriable name...

 

What is my mistake?

 

thank you very much in advance,

defaz

4 REPLIES 4
Loko
Barite | Level 11

Hello,

 

The problem is that you set no value for the macro variable :

%let &L_var._txt = ;

If you replace the statement , for example, with 

 

%let &L_var._txt = some;

you will notice the result is displayed correctly .

Patrick
Opal | Level 21

Here some code variations which should work:

%macro init_web_parameter(varlist);
  %do i=1 %to %sysfunc(countw(&varlist));
    %global %qscan(&varlist,&i);
    %put macro var is: &%qscan(&varlist,&i);
  %end;
%mend;

%init_web_parameter(sk_fg sk_fg_txt);
%put &=sk_fg;
%put &=sk_fg_txt;

 

You could also use parmbuff (example 3 under below link)

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p1nypovnwon4uyn15...

%macro init_web_parameter / parmbuff;
  %do i=1 %to %sysfunc(countw(&SYSPBUFF));
    %global %qscan(&SYSPBUFF,&i);
    %put macro var is: &%qscan(&SYSPBUFF,&i);
  %end;
%mend;

%init_web_parameter(purple red blue teal);

 

 

 

 

defaz
Fluorite | Level 6

hi Patrick,

 

I wrote an answer and my real problem while you were editing and changing your first reply...all my writing is in nirvana now...

Anyway, short story:

 

I can reference my newly created variables without problems:

%init_web_parameter(sk_fg sk_fg_txt);
%put &=sk_fg;
%put &=sk_fg_txt;

this works.

 

But how do I have to reference EXAMPLE_txt if I have the name "EXAMPLE" in another variable?

%let EXAMPLE_txt=;
%let xxx=EXAMPLE;
%let yyy=&&&xxx._txt;

so yyy contains &EXAMPLE_txt.....  (see my second log)

 

I expected yyy resulting in an empty string.

Where is my mistake?

thank you very much in advance,

defaz

Patrick
Opal | Level 21

The code I've posted creates as many macro variables as there are words in the string you pass to the macro. That's what I understood you're after.

 

"But how do I have to reference EXAMPLE_txt if I have the name "EXAMPLE" in another variable?"

I don't understand your question.

 

As for your code: You're creating macro variables in a loop but you have already a %put statement in there with fixed names - so that's firstly no more dynamic and 2nd that's why you get a Warning in the log during the first iteration of the macro loop. 

For your testing: Make sure that you always create a new SAS session so you don't miss issues due to macro variables already existing from an earlier test.

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
  • 4 replies
  • 1186 views
  • 0 likes
  • 3 in conversation