I was just recently exposed to %NRSTR, so I am not familiar enough to answer my own question.
I used to have a macro that would ask for two stratification labels (STRATLAB1 and STRATLAB2) as well as a group label (GROUPLAB). My tratification labels used to be FR(++) and FR(+) with a group label being "FR(++) vs. FR(+)". Our company has now changed the naming convention to be FR(100%) in place of FR(++) and FR(10-90%) in place of FR(+).
My macro call used to look like this (This is very simplified. My actual macro actually receives up to 25 parameters.):
%efficacyreport(stratlab1=%str(FR(++)), stratlab2=%str(FR(+)), grouplab1=%nrstr(FR(++) vs. FR(+)));
Now, I am trying to run it like this, which is giving me errors:
%efficacyreport(stratlab1=%nrstr(FR(100%)), stratlab2=%nrstr(FR(10-90%)), grouplab1=%nrstr(FR(100%) vs. FR(10-90%)));
Can someone tell me if I have a syntax error? I assume it is the % within the %NRSTR that is causing the problem becuase when I call the macro, the SAS log will not do anything (no errors, no warnings, no notes), and I cannot run other code. If I run ); four times, then it spits out errors:
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, INPUT, PUT.
The code inside the macro that is causing this macro looks like this (nothing wrong syntactically):
data efficacy;
set &efficacydata.;
strata=&strata.;
survtime=&survtime.;
censorflag=&censorflag.;
run;
where &efficacydata, &strata, &survtime, and &censorflag are input parameters with the macro. The error message has the semi-colons underlined with a 22, and then gives me the ERROR 22-322 message. I am sure that is has something to do with my % signs within the %NRSTR function. This is why I have to submit ); four times to close the macro call.
Any thoughts?
dj,
Inside the %str and %nrstr functions, special characters require special handling. You found one of them: % should be referred to as %%. However, others are in this category as well. ( should be referred to as %(, and ) as %). So technically, your original macro calls should have used:
stratlab1=%str(FR%(++%)),
However, when the parentheses are balanced, you can often get away with omitting the % before them. Once you added %, intending it to be a literal character, you actually changed the right parentheses by referring to it as %). Now the parentheses appear to macro language to be unbalanced. I consider it amazing that you actually figured out to add )))) to be able to continue processing.
Hope this helps!
Someone can correct me if I am wrong, but it looks like I have to have two % signs instead of one. I did that, and the macro ran fine. But can someone explain to me why that is?
dj,
Inside the %str and %nrstr functions, special characters require special handling. You found one of them: % should be referred to as %%. However, others are in this category as well. ( should be referred to as %(, and ) as %). So technically, your original macro calls should have used:
stratlab1=%str(FR%(++%)),
However, when the parentheses are balanced, you can often get away with omitting the % before them. Once you added %, intending it to be a literal character, you actually changed the right parentheses by referring to it as %). Now the parentheses appear to macro language to be unbalanced. I consider it amazing that you actually figured out to add )))) to be able to continue processing.
Hope this helps!
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!
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.