DATA Step, Macro, Functions and more

%STR() vs. %NRSTR()

Accepted Solution Solved
Reply
Regular Contributor
Posts: 237
Accepted Solution

%STR() vs. %NRSTR()

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?



Accepted Solutions
Solution
‎08-29-2012 10:25 AM
Super User
Posts: 5,518

Re: %STR() vs. %NRSTR()

Posted in reply to djbateman

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!

View solution in original post


All Replies
Regular Contributor
Posts: 237

Re: %STR() vs. %NRSTR()

Posted in reply to djbateman

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?

Solution
‎08-29-2012 10:25 AM
Super User
Posts: 5,518

Re: %STR() vs. %NRSTR()

Posted in reply to djbateman

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!

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 542 views
  • 3 likes
  • 2 in conversation