DATA Step, Macro, Functions and more

Using values from a dataset as macro variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Using values from a dataset as macro variables

Hi all,

I want to use the values of a variable in a dataset as macro variables in other procedure. I'm using the CALL SYMPUT option but something is wrong. Consider the follow example.


DATA nowanted;
infile datalines truncover;
input variables $ freq :$10.;
datalines;
Var3 100
Var6 100
Var8 100
run;


DATA fulldata;
INFILE datalines truncover;
INPUT (Var1 - Var8) ($);
datalines;
20 50 20 80 45 50 20 60
20 20 20 80 50 50 30 60
50 50 20 50 45 50 30 60
50 30 20 50 50 50 50 60
30 30 20 80 50 50 30 60
run;

 

DATA _null_;
SET nowanted;
CALL SYMPUT ("macrovar", variables);
run;


%put variables to drop from the fulldata are &macrovar;


DATA datahold; RUN; *empty data set to hold results;

DATA Final; SET datahold fulldata(DROP=&macrovar);
RUN;


In the results,the Final dataset the var8 is dropped but not the Var3 and Var6 variables as is expected.

 

Any suggestion?

 

Thanks


Accepted Solutions
Solution
‎04-04-2016 07:35 AM
Super User
Posts: 19,805

Re: Using values from a dataset as macro variables

Trace your code:

 

Line1 - set macrovar to var3

Line2 - set macrovar to var6

Line3 - set macrovar to var8

 

Macro variable resolves to var8 -> you've erased the previous values in each iteration.

 

You need to create multiple macro variables or use SQL to select them all into one macro variable. I recommend the SQL solution:

 

 

proc sql;
select variables into :var_list separated by " ";
quit;

%put &var_list;

View solution in original post


All Replies
Solution
‎04-04-2016 07:35 AM
Super User
Posts: 19,805

Re: Using values from a dataset as macro variables

Trace your code:

 

Line1 - set macrovar to var3

Line2 - set macrovar to var6

Line3 - set macrovar to var8

 

Macro variable resolves to var8 -> you've erased the previous values in each iteration.

 

You need to create multiple macro variables or use SQL to select them all into one macro variable. I recommend the SQL solution:

 

 

proc sql;
select variables into :var_list separated by " ";
quit;

%put &var_list;
☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 200 views
  • 1 like
  • 2 in conversation