BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

This is the macro program i am using
%MACRO SORT(DS= , KEY = );
proc sort data =&DS;
by &KEY ;
run;
%MEND SORT(DS= , KEY = );

While calling this macro, one of my column name has space in it. So i am using %bquote like this

%SORT(DS = MAPPINGFILE,KEY = %bquote('RH ID'n))

After this the macro variable key is getting resolved to 'RH ID'n but i am not able to sort the data and getting an error. It is saying column N could not be found..The foolowing is the log.............................
/**********************************************/
NOTE: Writing HTML(EGHTML) Body file: EGHTML
11 ODS HTML(ID=EGHTML) FILE=EGHTML ENCODING='utf-8' STYLE=EGDefault
11 ! STYLESHEET=(URL="file:///C:/Program%20Files/SAS/Shared%20Files/BIClientStyles/EGDefault.css")
11 ! ATTRIBUTES=("CODEBASE"="http://www2.sas.com/codebase/graph/v91/sasgraph.exe") NOGTITLE NOGFOOTNOTE GPATH=&sasworklocation
11 ! ;
SYMBOLGEN: Macro variable SYSVLONG resolves to 9.01.01M3P020206
SYMBOLGEN: Macro variable GRAPHAVAIL resolves to 9.01 TS1M3
12
13 %gaccessible;
SYMBOLGEN: Macro variable ACCESSIBLE resolves to ACCESSIBLE
14 %SORT(DS = MAPPINGFILE,KEY = %bquote('RH ID'n));
SYMBOLGEN: Macro variable DS resolves to MAPPINGFILE
SYMBOLGEN: Macro variable KEY resolves to 'RH ID'n
SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.
NOTE: Line generated by the macro variable "KEY".
14 'RH ID'n
_
22
__
202
ERROR: Variable N not found.

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, DECENDING, DESCENDING, DESENDING, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.

ERROR 202-322: The option or parameter is not recognized and will be ignored.
/********************************************/

So please tell how can i invoke this macro if column name is having space?
8 REPLIES 8
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You want to use the %STR macro function.

Scott Barry
SBBWorks, Inc.

SAS Macro Language: Reference, %STR and %NRSTR Functions
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001061290.htm
deleted_user
Not applicable
Hi Scott

I ahve already tried that one but it doesnt work.

%SORT(DS = MAPPINGFILE,KEY = %str(%'RH ID%'n))

Nd wats the difference if i use %str like the mentioned above and %bqoute which i have posted earlier...
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
However, I ran your code with %STR( ) coded and it sure worked for me. Reply with a pasted SAS log where the use of %str(...) does not work, please.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Hi Scott

/*This is my code and i am using sas eg 4.1*/
data test;
infile datalines dlm = '|' dsd;
input name :$ 'rh id'n ;
datalines;
a|2
b|3
c|7
d|1
e|0
f|4
;
run;
%macro sort(ds1=,key=);
PROC SORT DATA = &DS1;
BY &KEY;
RUN;
%mend sort;
%sort(ds1 = test,key = %str(%'rh id%'n));
/* After this i am getting error:::
ODS HTML(ID=EGHTML) FILE=EGHTML ENCODING='utf-8' STYLE=EGDefault
11 ! STYLESHEET=(URL="file:///C:/Program%20Files/SAS/Shared%20Files/BIClientStyles/EGDefault.css")
11 ! ATTRIBUTES=("CODEBASE"="http://www2.sas.com/codebase/graph/v91/sasgraph.exe") NOGTITLE NOGFOOTNOTE GPATH=&sasworklocation
11 ! ;
12
13 %gaccessible;
14 %macro sort(ds1=,key=);
15 PROC SORT DATA = &DS1;
16 BY &KEY;
17 RUN;
18 %mend sort;
19 %sort(ds1 = test,key = %str(%'rh id%'n));
ERROR: Variable N not found.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.02 seconds
user cpu time 0.00 seconds
system cpu time 0.01 seconds
Memory 20k
Page Faults 1
Page Reclaims 0
Page Swaps 0
Voluntary Context Switches 1
Involuntary Context Switches 1
Block Input Operations 0
Block Output Operations 0

NOTE: Line generated by the macro variable "KEY".
19 'rh id'n
_
22
__
202
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, DECENDING, DESCENDING, DESENDING, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
20
21 %LET _CLIENTTASKLABEL=;
22 %LET _EGTASKLABEL=;
23 %LET _CLIENTPROJECTNAME=;
24 %LET _SASPROGRAMFILE=;
25
26 ;*';*";*/;quit;run;
27 ODS _ALL_ CLOSE;
2 The SAS System 10:04 Wednesday, March 24, 2010

28
29
30 QUIT; RUN;
31
*/
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Your code changed from the first post. Instead of %bquote, use %str with the first code example and it will work, as I suggested.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
The code is same Scott. I have just changed the macro variable name. If you want it that way only,then Check out this code::::::::::::::::::::::::::::::::::::

data test;
infile datalines dlm = '|' dsd;
input name :$50. 'rh id'n ;
datalines;
a|2
i|3
c|7
d|1
e|0
f|4
;
run;
%MACRO SORT(DS= , KEY = );
proc sort data =&DS;
by &KEY ;
run;
%MEND SORT(DS= , KEY = );
%SORT(DS = test,KEY = %str(%'RH ID%'n))
/*************************************************************/

And the log is as follow::::::::::::::::::::::::::::::::

/*************************************/
NOTE: The data set WORK.TEST has 6 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
Memory 292k
Page Faults 2
Page Reclaims 0
Page Swaps 0
Voluntary Context Switches 6
Involuntary Context Switches 1
Block Input Operations 0
Block Output Operations 0


24 ;
25 run;
26 %MACRO SORT(DS= , KEY = );
27 proc sort data =&DS;
28 by &KEY ;
29 run;
30 %MEND SORT(DS= , KEY = );
WARNING: Extraneous information on %MEND statement ignored for macro definition SORT.
31 %SORT(DS = test,KEY = %str(%'RH ID%'n))
ERROR: Variable N not found.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.01 seconds
Memory 20k
Page Faults 0
Page Reclaims 0
2 The SAS System 10:04 Wednesday, March 24, 2010

Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 0

NOTE: Line generated by the macro variable "KEY".
31 'RH ID'n
_
22
__
202
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, DECENDING, DESCENDING, DESENDING, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
32
33 %LET _CLIENTTASKLABEL=;
34 %LET _EGTASKLABEL=;
35 %LET _CLIENTPROJECTNAME=;
36 %LET _SASPROGRAMFILE=;
37
38 ;*';*";*/;quit;run;
39 ODS _ALL_ CLOSE;
40
41
42 QUIT; RUN;
43
/****************************************************************/
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suit yourself - the code is not the same as initial post - check it out from your original post, you used:

%SORT(DS = MAPPINGFILE,KEY = %bquote('RH ID'n))

which will work if coded as:

%SORT(DS = MAPPINGFILE,KEY = %str('RH ID'n))

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Thanks Scott

And i havnt changed the code. Just the name of the dataset was changed. And for knowledge i want to know
If i am using this ::
%SORT(DS = MAPPINGFILE,KEY = %str('RH ID'n));
then its working
But if i use these::::
%SORT(DS = MAPPINGFILE,KEY = %bquote('RH ID'n));
or
%SORT(DS = MAPPINGFILE,KEY = %str(%'RH ID%'n));

Then it is showing error but why so..coz %bquote or %str(%'RH ID%'n), as i think are to mask qoutes in a string .....

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1188 views
  • 0 likes
  • 2 in conversation