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.
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...
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.
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?
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.
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.
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...
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)));
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.