Hi, I am new to SAS and am using 9.2.
I would like to read in variables from a dataset, based on a %let statement. A simplification of the issue follows, but I get an error.
I'd appreciate someone's help.
Thanks,
Brent Fulton
UC Berkeley
%let type=color;
if &type='color' then do;
%let varlist=red blue;
end;
if &type='pattern' then do;
%let varlist=solid stripe;
end;
run;
data data1 (keep= &varlist);
set datalib.data1;
run;
For something so simple you can also just use a DATA _NULL_ step and not bother with macro language.
%let type=color;
%let varlist=;
data _null_;
if "&type" = 'color' then call symput('varlist','red blue');
if "&type" = 'pattern' then call symput('varlist','solid stripe');
run;
data data1 (keep= &varlist);
set datalib.data1;
run;
Maybe it will help you, but NOTE that you can use %if statement only in %macro xxx; .... %mend xxx; statement
%macro mMain;
%let type=color;
%if &type=%str(color) %then %do;
%let varlist=red blue;
%end;
%if &type=%str(pattern) %then %do;
%let varlist=solid stripe;
%end;
data data1(keep= &varlist);
set datalib.data1;
run;
%mend mMain;
%mMain
For something so simple you can also just use a DATA _NULL_ step and not bother with macro language.
%let type=color;
%let varlist=;
data _null_;
if "&type" = 'color' then call symput('varlist','red blue');
if "&type" = 'pattern' then call symput('varlist','solid stripe');
run;
data data1 (keep= &varlist);
set datalib.data1;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.