BookmarkSubscribeRSS Feed
ilikesas
Barite | Level 11

Hi,

suppose I have the following macro:

%MACRO sortandprint(sortseq=, sortvar=);

PROC SORT DATA = models;

BY &sortseq &sortvar;

PROC PRINT DATA = models NOOBS;

TITLE 'Current Models';

TITLE2 "Sorted by &sortseq &sortvar";

VAR Model Class Frame Price;

RUN;

%MEND sortandprint;


Is it possible to generalize this macro even further in the following ways:


1) also create a macro variable for the name of the data file so that have the following:

%MACRO sortandprint(datafile=, sortseq=, sortvar=);

PROC SORT DATA = &datafile;


2) put all the variables of the datfile into a list so that get something like the following:

var &varlist



thank you!

2 REPLIES 2
ballardw
Super User

You are on the right path for datafile, it should work as shown. Just remember that you may need to include the library and set: datafile=mylib.thisdataset but that will work. Generally I will make a separate Library reference but that is a style choice as much as anything. Though if you are referencing many sets in the same library it may be more efficient.

Add a parameter varlist to the macro call:

%MACRO sortandprint(datafile=, sortseq=, sortvar=, varlist=);

and call as

%sortandprint(datafile=datasetname, sortseq=, sortvar=, varlist= thisvar thatvar anothervar);

NOTE that the comma delimits the parameters. So the list shouldn't contain commas in general.

If you need at some point to pass the varlist with commas then use

varlist=%str( thisvar, thatvar, anothervar) in the macro call.

Reeza
Super User

1) Yes - try your code

2) Yes - but also look at _all_ _numeric_ variable lists. You also don't need to specify Var in proc print and then it prints everything.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 834 views
  • 0 likes
  • 3 in conversation