macro %str() in passing variable trouble

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

macro %str() in passing variable trouble

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

 


Accepted Solutions
Solution
‎10-09-2017 09:56 AM
Super User
Super User
Posts: 7,164

Re: macro %str() in passing variable trouble

[ Edited ]
Posted in reply to donghuazeng

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


All Replies
Solution
‎10-09-2017 09:56 AM
Super User
Super User
Posts: 7,164

Re: macro %str() in passing variable trouble

[ Edited ]
Posted in reply to donghuazeng

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);
New Contributor
Posts: 4

Re: macro %str() in passing variable trouble

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

Super User
Super User
Posts: 7,164

Re: macro %str() in passing variable trouble

Posted in reply to donghuazeng

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.
Super User
Posts: 11,578

Re: macro %str() in passing variable trouble

Posted in reply to donghuazeng

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);

New Contributor
Posts: 4

Re: macro %str() in passing variable trouble

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

 

 

Super User
Super User
Posts: 7,164

Re: macro %str() in passing variable trouble

Posted in reply to donghuazeng

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.

New Contributor
Posts: 4

Re: macro %str() in passing variable trouble

excellent!   very much appreicated

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 121 views
  • 0 likes
  • 3 in conversation