BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi!

I will like to know if it's possible to program a MACRO with a variable number of parametres, and if it's possible how can I do it.

I am trying to order a DATASET with PROC SORT and I need that the variables by which I am ordering the dataset could be variable, in number and in value.

Sorry for my English.

Regards.
3 REPLIES 3
LawrenceHW
Quartz | Level 8
Javi,

The best way of achieving this is have a macro variable which is a list of data set variables (i.e. SortOrder=var1 var2 var3).

If you want to be a bit more dynamic then you probably need to utilise the metadata available about data sets (using PROC CONTENTS or the SASHELP data views) to find out what variables exist on data sets.

Hope this helps,
Lawrence
Cynthia_sas
Diamond | Level 26
Javi:
Another point to add to Lawrence's answer is that if a macro variable has no value, when substitution is done, you could consider it to be the same as adding a blank or hit on the space bar to your code.

The first time this Proc Sort is invoked, there is no value for var3 (%let var3=;) so the data is only sorted by region and subsidiary. For the second Proc Sort, all 3 macro variables have a value, so the data is sorted by all 3 variables.

cynthia

[pre]
options mprint mlogic symbolgen;
%let var1 = region;
%let var2 = subsidiary;
%let var3 =;
proc sort data=sashelp.shoes out=shoes1;
by &var1 &var2 &var3;
run;

options mprint mlogic symbolgen;
%let var1 = product;
%let var2 = region;
%let var3 =subsidiary;
proc sort data=sashelp.shoes out=shoes2;
by &var1 &var2 &var3;
run;

[/pre]
deleted_user
Not applicable
Thank you very much!!

I will try your solutions

Javi.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 3 replies
  • 1633 views
  • 0 likes
  • 3 in conversation