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);
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;
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.
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?
@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.
%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);
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;
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.
@Citrine10 wrote:
ok but how will it work with some special characters and mnemonics as %NRSTR masks & and %.
Provide examples.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.