DATA Step, Macro, Functions and more

if statement with %let

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

if statement with %let

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;


Accepted Solutions
Solution
‎10-01-2011 09:18 AM
Super User
Super User
Posts: 6,502

Re: if statement with %let

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;

View solution in original post


All Replies
New Contributor
Posts: 3

Re: if statement with %let

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

Solution
‎10-01-2011 09:18 AM
Super User
Super User
Posts: 6,502

Re: if statement with %let

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;

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 4779 views
  • 4 likes
  • 3 in conversation