BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
donghuazeng
Calcite | Level 5

I had following code but dataset name won't take passed macro parameters with %str():

 

%macro test(prim =, doselv =);

data Prim_&prim._ (Keep = VARIABLE &DOSELV);;

set mock.&PRIM.;

run;

%mend;

%test(prim =%str(T_AE21110P3), doselv =C1);

 

Problem: prim_T_AE21110p3_ was not created as expected. only prim_ was in WORK folder.

log:

NOTE: There were 2 observations read from the data set MOCK.T_AE21110P3.

NOTE: The data set WORK.PRIM_ has 2 observations and 22 variables.

NOTE: The data set WORK.T_AE21110P3_ has 2 observations and 2 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The macro quoting is confusing the SAS parser and it is seeing the concatenated dataset name as multiple tokens.

You could remove the macro quoting using %unquote().

%macro test(prim =, doselv =);
data %unquote(Prim_&prim._) (Keep = VARIABLE &DOSELV);;
set %unquote(mock.&PRIM);
run;
%mend;
%test(prim =%str(T_AE21110P3), doselv =C1);

Frequently I find you can also solve this by just using a local macro variable to hold the generated name.

%macro test(prim =, doselv =);
%local target source;
%let target=%unquote(Prim_&prim._);
%let source=%unquote(mock.&PRIM.);
data  &target (Keep = VARIABLE &DOSELV);;
set &source;
run;
%mend;
%test(prim =%str(T_AE21110P3), doselv =C1);

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

The macro quoting is confusing the SAS parser and it is seeing the concatenated dataset name as multiple tokens.

You could remove the macro quoting using %unquote().

%macro test(prim =, doselv =);
data %unquote(Prim_&prim._) (Keep = VARIABLE &DOSELV);;
set %unquote(mock.&PRIM);
run;
%mend;
%test(prim =%str(T_AE21110P3), doselv =C1);

Frequently I find you can also solve this by just using a local macro variable to hold the generated name.

%macro test(prim =, doselv =);
%local target source;
%let target=%unquote(Prim_&prim._);
%let source=%unquote(mock.&PRIM.);
data  &target (Keep = VARIABLE &DOSELV);;
set &source;
run;
%mend;
%test(prim =%str(T_AE21110P3), doselv =C1);
donghuazeng
Calcite | Level 5

Thanks, Tom!

it is very helpful.  I realized that %str() here is not necessary. Is it particularly sensitive when SAS takes it as a dataset name? I mean if I pass a macro variable  with %str() for non-dataset name, do I still have this trouble?

 

in this case, %str() only acted negatively for newly created dataset: work.prim_&prim_,  but existing dataset Mock.&prim

 has not such a trouble.

 

 

Best

Don

Tom
Super User Tom
Super User

@donghuazeng wrote:

Thanks, Tom!

it is very helpful.  I realized that %str() here is not necessary. Is it particularly sensitive when SAS takes it as a dataset name? I mean if I pass a macro variable  with %str() for non-dataset name, do I still have this trouble?

 

in this case, %str() only acted negatively for newly created dataset: work.prim_&prim_,  but existing dataset Mock.&prim

 has not such a trouble.

 

 

Best

Don


I suspect that the difference is that in the first you are trying to build one word from a combination of text and a macro expression. But in the second the whole member name is coming from the macro variable. I suspect that SAS is already parsing the libref period and member name as three different tokens and so the macro quoting is not confusing.

5    %let x=%str(ss);
6    %let y=%str(class);
7    proc print data=sashelp.cla&x ; run;
ERROR: File SASHELP.CLA.DATA does not exist.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.06 seconds
      cpu time            0.00 seconds

NOTE: Line generated by the macro variable "X".
7    sashelp.class
                --
                22
                 --
                 202

ERROR 22-322: Syntax error, expecting one of the following: ;, (, BLANKLINE, CONTENTS, DATA,
              DOUBLE, GRANDTOTAL_LABEL, GRANDTOT_LABEL, GRAND_LABEL, GTOTAL_LABEL, GTOT_LABEL,
              HEADING, LABEL, N, NOOBS, NOSUMLABEL, OBS, ROUND, ROWS, SPLIT, STYLE, SUMLABEL,
              UNIFORM, WIDTH.

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


8    proc print data=sashelp.&y ; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
ballardw
Super User

The example you use has no need for the %str to pass a value.

Do you have a more complex version that you can share that might actually require the use of %str ? If not then use

 

%test(prim = T_AE21110P3, doselv =C1);

donghuazeng
Calcite | Level 5

HI, Tom:

I set the options of symbolgen mprint mlogic and passed the same %str(Prim)

1) it is strange that the log showed data.prim_&prim was correctly resolved. however, there are notes of "unquoted ....";

    and later the WORK folder still has Prim_  (missed &prim)

2) if I did data step as: data.&prim_, it will resolve &prim. but with note of "unquoted...."

this may be the case when you said "concatenate":  any leading letter for &prim will cause missing of &prim if it is passed with %str().

 

thanks you so much for clarification.

 

 

Best

Don

 

 

Tom
Super User Tom
Super User

It does NOT physically add a space between the strings it just internally thinks that the string is multiple words because of the presence of the macro quoting.

 

That is what is so confusing about this error since the generated string looks right but SAS is not interpreting it right.

donghuazeng
Calcite | Level 5

excellent!   very much appreicated

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 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
  • 7 replies
  • 537 views
  • 0 likes
  • 3 in conversation