DATA Step, Macro, Functions and more

Weird nrquote behaviour

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

Weird nrquote behaviour

Consider this snippet:

 

%let V = %nrquote(LONG TEXT);

%put "&V";    /* OK */
%put "&V"n;   /* WHAT?! */

The output of the first put is:

"LONG TEXT"

which is correct.

 

The output of the second put is

"LONG TEXT"n

which contains some very weird characters at after and before the double quotes.

I would expect to see the output of the second put as:

"LONG TEXT"n

What is happening?!?

 

I need this to execute a data step:

data WORK.&V;
run;

where the table is given as an input and the name may contain spaces.


Accepted Solutions
Solution
‎10-31-2017 05:03 AM
PROC Star
Posts: 1,428

Re: Weird nrquote behaviour

Posted in reply to ChrisHemedinger

Ahh, thanks for the correction, @ChrisHemedinger.  That's what I get for posting while listening to a conference call...

 

So for those who want to live dangerously, looks like you could set validmemname=extend, and then the only issue is you would need to %unquote() the value of the data set name, e.g.:

 

89   %let OUTPUT_TABLE = %nrquote(TABLE 1);
90   options validmemname=extend;
91
92   %put >>&Output_Table<<;
>>TABLE 1<<
93
94   data "&OUTPUT_TABLE"n;
95     set sashelp.class;
96   run;

ERROR: Invalid physical name for library WORK.
NOTE: The SAS System stopped processing this step because of errors.

97
98
99   data "%unquote(&OUTPUT_TABLE)"n;
100    set sashelp.class;
101  run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.'TABLE 1'n has 19 observations and 5 variables.

 

As long as you don't have any macro triggers in the name, might be acceptable.  For risk takers...

View solution in original post


All Replies
Super User
Posts: 9,551

Re: Weird nrquote behaviour

[ Edited ]

These are artifacts of the macro quoting functions, which are later used by the macro processor while resolving/non-resolving.

 

This works:

%let V = LONG TEXT;

data work."&v."n;
set sashelp.class;
run;

Thou shalt not use non-standard names for SAS datasets. Unless you have a really strong masochistic tendency.

 

And if you have to use quoting functions to mask macro triggers(!) in dataset names, you're very far out in ga-ga land.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 48

Re: Weird nrquote behaviour

Posted in reply to KurtBremser

Truth is, I'm developing a job in Data Integration, and I let the user to choose the output table name in a text prompt.

Looking at the job's generated code, I see that DI puts the prompt value in %nrquote. I don't need that, however so it is.

Moreover, some legacy output tables have been created with spaces so I must make it work also in this case.

 

So: the user inserts in the prompt the output table name text. I configured DI to put this text into the macrovar &OUTPUT_TABLE.

Suppose the user inserts TABLE 1. I see in the DI autogenerated code this:

%let OUTPUT_TABLE = %nrquote(TABLE 1);

just before my custom code.

 

Then when I try to use a datastep with this macrovar, neither

data &OUTPUT_TABLE;
  set ...;
run;

nor

data "&OUTPUT_TABLE"n;
  set ...;
run;

works.

 

 

I tried your code, it runs successfully however the table generated contains the weird characters in its name! That is, the table name is really:

WORK.LONG TEXT

What would you recommend me to do?

PROC Star
Posts: 1,428

Re: Weird nrquote behaviour

Spaces are not allowed inside data set names, e.g.:

 

29   options validvarname=any;
30
31   data "Table 1"n;
32     set sashelp.class;
33   run;

ERROR: The value 'TABLE 1'n is not a valid SAS name.
NOTE: The SAS System stopped processing this step because of errors.

 

If your users don't know this, and you want to allow them to enter spaces, I think you'll have to fix the name for them.

 

If you don't need the macro quoting, you could %sysfunc(compress()) the data set name, e.g. :

 

46   %let OUTPUT_TABLE = %nrquote(TABLE 1);
47   %let OUTPUT_TABLE = %sysfunc(compress(&Output_Table));
48
49   data &Output_Table;
50     set sashelp.class;
51   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TABLE1 has 19 observations and 5 variables.
Community Manager
Posts: 3,349

Re: Weird nrquote behaviour

Spaces are allowed in member names when you set the VALIDMEMNAME option (new in 9.4).  But I agree with everyone who advises against it -- this option is more about preserving names when reading from other sources from other systems with their own crazy permissive rules.

Solution
‎10-31-2017 05:03 AM
PROC Star
Posts: 1,428

Re: Weird nrquote behaviour

Posted in reply to ChrisHemedinger

Ahh, thanks for the correction, @ChrisHemedinger.  That's what I get for posting while listening to a conference call...

 

So for those who want to live dangerously, looks like you could set validmemname=extend, and then the only issue is you would need to %unquote() the value of the data set name, e.g.:

 

89   %let OUTPUT_TABLE = %nrquote(TABLE 1);
90   options validmemname=extend;
91
92   %put >>&Output_Table<<;
>>TABLE 1<<
93
94   data "&OUTPUT_TABLE"n;
95     set sashelp.class;
96   run;

ERROR: Invalid physical name for library WORK.
NOTE: The SAS System stopped processing this step because of errors.

97
98
99   data "%unquote(&OUTPUT_TABLE)"n;
100    set sashelp.class;
101  run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.'TABLE 1'n has 19 observations and 5 variables.

 

As long as you don't have any macro triggers in the name, might be acceptable.  For risk takers...

Super User
Super User
Posts: 7,845

Re: Weird nrquote behaviour

If you want to use non-standard member names then set VALIDMEMNAME=EXTEND.

If you want to convert user entered text to a name literal then use the NLITERAL() function.

%let OUTPUT_TABLE = %nrquote(TABLE 1);

%let output_table_x=%sysfunc(nliteral(%superq(output_table)));
 
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 174 views
  • 3 likes
  • 5 in conversation