Hi ,
I have a dataset which looks like below . Here k_cnt = 3 and here I am unable to resolve this &&c_&&k_cnt&i.._pls . So is that something error in the way I have written ?
| c_apple_cols | c_orange_cols | c_berry_cols |
| 34 | 45 | 0 |
%macro test2;
data _null_;
set test;
call symputx('k_lst'||left(_n_),_NAME_);
if last then call symput('k_cnt',_n_);
run;
proc sql;
select
%do i=1 %to &k_cnt;
c_&&k_lst&i.._cols,
total_items
%end;
into
%do i=1 %to &k_cnt.;
:c_&&k_lst&i.._pls,
%end;
:total_pls
from testg;
quit;
%do i=1 %to &k_cnt
%If &&c_&&k_lst&i.._pls > 0 %then %do;
%let u = 'Amazing';
%end;
%else %do;
%let u = 'Super';
%end;
%end;
%mend;
%test2;
Can anyone please help
This expression is incorrect:
&&c_&&k_cnt&i.._pls
Because you need double-resolution inside the string (&&k_cnt&i..), you need triple resolution for the entire string. So you will need four ampersands:
&&&&c_&&k_cnt&i.._pls
You haven't defined any macro variables that would map 1 to APPLE.
Why not just skip the macro and use a data step to generate the macro variables?
data _null_;
set have ;
array x _numeric_;
do i=1 to dim(x);
call symputx(vname(x(i)),x(i));
end;
run;
Results:
80 %Put &=c_apple_cols &=c_orange_cols &=c_berry_cols &=total_items ; C_APPLE_COLS=34 C_ORANGE_COLS=45 C_BERRY_COLS=0 TOTAL_ITEMS=79
Hi Tom,
I have replied the code I have used to create the macro variables
Thank you
This expression is incorrect:
&&c_&&k_cnt&i.._pls
Because you need double-resolution inside the string (&&k_cnt&i..), you need triple resolution for the entire string. So you will need four ampersands:
&&&&c_&&k_cnt&i.._pls
Hi ,
Thank you so much for all your help .thanks a lot . This one worked
Thank you
There are usually much better ways to generate code than using macro "arrays".
I think your macro variables are getting generated but perhaps you don't know how to reference them?
%let i=1 ;
%let k_lst1 = Apple ;
%put c_&&k_lst&i.._cols ;
%let c_Apple_cols = 65 ;
%put &&&&c_&&k_lst&i.._cols ;
212 options symbolgen; 213 %put &&&&c_&&k_lst&i.._cols ; SYMBOLGEN: && resolves to &. SYMBOLGEN: && resolves to &. SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 1 SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable K_LST1 resolves to Apple SYMBOLGEN: Macro variable C_APPLE_COLS resolves to 65 65 214 options nosymbolgen;
I find that it is usually a lot easier to build up the result in pieces instead of generating such a complex expression to resolve a macro variable.
215 %let name = c_&&k_lst&i ; 216 %let name = &name._cols ; 217 %let value=&&&name ; 218 %put &=name &=value; NAME=c_Apple_cols VALUE=65
Hi ,
Thanks a lot for all your help . This really helped me
Thank you so much
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.