BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
randomman
Fluorite | Level 6

data a;
input name $ age height weight;
datalines;
don 28 168 76
;run;
%macro cols1; name age; %mend;
%macro cols2; height weight; %mend;
proc print data = a; var %cols2 %cols1; run;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
Opal | Level 21
Calling %cols2 generates all the text within the macro:

height weight;

Extrapolating upon this, your full VAR statement generates:

var height weight; name age;

Clearly, there's an extra semicolon. A good solution would be to remove the generated semicolons from the macro definition. Have %cols2 generate only:

height weight

Then add a semicolon for the VAR statement when calling the macros:

var %cols2 %cols1 ;

View solution in original post

2 REPLIES 2
Astounding
Opal | Level 21
Calling %cols2 generates all the text within the macro:

height weight;

Extrapolating upon this, your full VAR statement generates:

var height weight; name age;

Clearly, there's an extra semicolon. A good solution would be to remove the generated semicolons from the macro definition. Have %cols2 generate only:

height weight

Then add a semicolon for the VAR statement when calling the macros:

var %cols2 %cols1 ;
Patrick
Opal | Level 21

Remove the semicolons and your code will work.

Patrick_0-1692585470737.png

 

You normally only use macros for dynamic code generation. 

For your case you'd rather use macro variables as in below sample code.

%let cols1=name age;
%let cols2=height weight;

proc print data = a;
  var &cols2 &cols1;
run;

 A macro definition for your sample could look like:

%macro doit(cols1,cols2);
  proc print data = a;
    var &cols2 &cols1;
  run;
%mend;

%doit(name age, height weight);

And as an advice: Don't dive too early into SAS macro coding. First get "solid" with SAS data step and SAS Proc's. 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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