%macro psql(dsname= ,x=);
proc sql;
select Name ,sex, Age from &dsname order by &x;
quit;
%mend;
*calling ;
;
%psql(dsname=sashelp.class,%str(x=sex, x=age desc));
Since you only need to mask the comma, the %STR function is sufficient:
%psql(dsname=sashelp.class,x=%str(sex,age desc));
Please try the below call
%psql(dsname=sashelp.class,x=%nrstr(sex, age desc));
Since you only need to mask the comma, the %STR function is sufficient:
%psql(dsname=sashelp.class,x=%str(sex,age desc));
You need to use a WHERE condition in the SQL select.
Start by creating the necessary SQL code without macro coding, once it works, you can make it dynamic.
Agreeing with @Kurt_Bremser . You can't write macros and expect them to work until you have working code without macros.
So, here is your code when run as
%psql(dsname=sashelp.class,x=%str(sex ='F',Age desc))
&DSNAME is replaced by sashelp.class and &X is replaced by %str(sex='F',age desc), producing this incorrect SQL code
proc sql;
select Name ,sex, Age from sashelp.class order by sex='F',Age desc;
quit;
Can you see why this SQL code doesn't work? So first, you have to write correct SQL code without macros and without macro variables, and fix the problems in the code immediately above. Show us correct SQL code that works, without macros and without macro variables, for this situation.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.