BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
	%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));

1 ACCEPTED SOLUTION
5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

Please try the below call

 

%psql(dsname=sashelp.class,x=%nrstr(sex, age desc));
Thanks,
Jag
BrahmanandaRao
Lapis Lazuli | Level 10
I Did not get only F values its give M and F both

%psql(dsname=sashelp.class,x=%str(sex ='F',Age desc));
Kurt_Bremser
Super User

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.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1165 views
  • 0 likes
  • 4 in conversation