BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Citrine10
Obsidian | Level 7

Hi there 

 

I am trying to use %nrstr and %str. I am passing my variable in the function however it wont work when trying to reference the value of the variable

 

e.g. 

%let myvar = crazy&609

%let maskedname = %nrstr(&myvar.);
%put &maskedfile.;

 

the masking works fine when doing this but not when using the variable:

%let maskedname = %nrstr(crazy&609);
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

For this example, no quoting or %STR/%NRSTR is needed, but it will work with quoting

 

%let myvar=craz!609;
%put The value of MYVAR is %bquote(&myvar);
%put The value of MYVAR is &myvar;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

To use &MYVAR, you need to quote the variable

 

%let myvar=crazy&609;
%put The value of MYVAR is %bquote(&myvar);

%STR and %NRSTR should be used when you can see the special character in the text inside %STR() or %NRSTR(). In this case, when you use &myvar, you can't see the special character, you see a macro variable &myvar which may or may not contain special characters, so the quoting functions are needed here.

--
Paige Miller
Citrine10
Obsidian | Level 7

Hi Paige, thank you

 

I would need to use NRSTR as  i have special characters in the name of the variable. What other alternatives are there?

PaigeMiller
Diamond | Level 26

@Citrine10 wrote:

Hi Paige, thank you

 

I would need to use NRSTR as  i have special characters in the name of the variable. What other alternatives are there?


Provide an example. 

--
Paige Miller
Citrine10
Obsidian | Level 7
%let myvar=crazy&609;
%put The value of MYVAR is %nrstr(&myvar);

or

%let myvar=craz!609;
%put The value of MYVAR is %nrstr(&myvar);
PaigeMiller
Diamond | Level 26

For this example, no quoting or %STR/%NRSTR is needed, but it will work with quoting

 

%let myvar=craz!609;
%put The value of MYVAR is %bquote(&myvar);
%put The value of MYVAR is &myvar;
--
Paige Miller
Citrine10
Obsidian | Level 7
ok but how will it work with some special characters and mnemonics as %NRSTR masks & and %.
Quentin
Super User

You still need to give an example with mnemonics you are trying to mask.  If you're trying to mask & or % which result from resolving a macro variable and might be seen as a macro trigger, then you can use %superq, e.g.:

 

%let myvar=craz&foo;   *will throw warning foo not resolved ;
%put The value of MYVAR is %superq(myvar); *no warning because & is masked ;

 

Note that masking of & or % is only needed when they would be seen as macro trigger.  The macro processor knows that a macro name and macro variable name must start with a letter, so &609 or %609 cannot be seen as macro triggers. 

%let myvar=craz&609; 
%put The value of MYVAR is &myvar;

%let myvar=craz%609; 
%put The value of MYVAR is &myvar;

Depending on how you use the macro variable, it's possible that in &609 the & might be seen as an AND operator.

 

Macro quoting is a hard subject.  It's an almost impossible subject to try to learn without an example.

 

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
PaigeMiller
Diamond | Level 26

@Citrine10 wrote:
ok but how will it work with some special characters and mnemonics as %NRSTR masks & and %.

Provide examples.

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1615 views
  • 0 likes
  • 3 in conversation