BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Edoedoedo
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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...

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

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.

Edoedoedo
Pyrite | Level 9

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?

Quentin
Super User

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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Quentin
Super User

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...

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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