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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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