BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrentFulton
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Hayk
Calcite | Level 5

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

Tom
Super User Tom
Super User

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;

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
  • 2 replies
  • 17816 views
  • 7 likes
  • 3 in conversation