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

Dear Sir,

Dear Madam,

 

Is there a way to use a variable with a SAS name literal as its name in a TABULATE procedure?

 

As an illustration, consider the following SAS code:

 

%LET LftdKwdr = %SYSFUNC(NLITERAL(Leeftijd ** 2%STR(,) (of Age ** 2)));
DATA Klas;
  SET Sashelp.Class;
  &LftdKwdr = Age ** 2;
%LET SxAgLKw = Sex Age &LftdKwdr;
PROC TABULATE DATA = Klas;
  CLASS &SxAgLKw;
  TABLE ALL &SxAgLKw, ALL;
RUN;

 

When I run the above code (in SAS 9.4), I get the following error message:

  ERROR: The type of name ('Leeftijd ** 2, (of Age ** 2)"'n) is unknown.

 

Here is a screenshot:

 

Schermafbeelding 2023-10-11 171048.png

 

Even if I first execute the OPTIONS statement below, I get the same error.

 

OPTIONS VALIDVARNAME = ANY;

 

How can I avoid this error while still keeping the variable name "Leeftijd ** 2, (of Age ** 2)"N?

 

Thanks in advance.

 

Peter Boonants

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi

 

Have you tried using the quote() function instead of the nliteral() function?

 

For example, try replacing:

 

 

%LET LftdKwdr = %SYSFUNC(NLITERAL(Leeftijd ** 2%STR(,) (of Age ** 2)));

 

with (note the extra "n" at the end):

 

 

%LET LftdKwdr = %SYSFUNC(quote(Leeftijd ** 2%STR(,) (of Age ** 2)))n;

 

 

 

Thanks & kind regards,

Amir.

View solution in original post

6 REPLIES 6
Amir
PROC Star

Hi

 

Have you tried using the quote() function instead of the nliteral() function?

 

For example, try replacing:

 

 

%LET LftdKwdr = %SYSFUNC(NLITERAL(Leeftijd ** 2%STR(,) (of Age ** 2)));

 

with (note the extra "n" at the end):

 

 

%LET LftdKwdr = %SYSFUNC(quote(Leeftijd ** 2%STR(,) (of Age ** 2)))n;

 

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

Works for me.

%LET LftdKwdr = 'Leeftijd ** 2, (of Age ** 2)'n;
%LET SxAgLKw = Sex Age &LftdKwdr;

options validvarname=any;

DATA Klas;
  SET Sashelp.Class;
  &LftdKwdr = Age ** 2;
run;
PROC TABULATE DATA = Klas;
  CLASS &SxAgLKw;
  TABLE ALL &SxAgLKw, ALL;
RUN;

options validvarname=v7;

But WHY make your life HARDER?

Use a valid SAS name for the variable.  Attach a label if you want something "pretty" in the output.

options validvarname=v7;
%LET SxAgLKw = Sex Age LftdKwdr;

DATA Klas;
  SET Sashelp.Class;
  LftdKwdr = Age ** 2;
  label  LftdKwdr = 'Leeftijd ** 2, (of Age ** 2)';
run;
PROC TABULATE DATA = Klas;
  CLASS &SxAgLKw;
  TABLE ALL &SxAgLKw, ALL;
RUN;
Peter_Boonants
Fluorite | Level 6

First of all, Tom and Amir, thanks a lot for your replies!

 

I did some further testing and found that a SAS name literal of the form "..."N or '...'N, where an upper case letter N immediately follows after the right quotation mark, apparently always causes an error in the present context.

This is in contrast to a SAS name literal of the form "..."n or '...'n, with the right quotation mark immediately followed by a lower case letter n.

 

For example, the expressions below that are equal to "..."N or '...'N, lead to an error:

  • "..."N
  • '...'N
  • %SYSFUNC(QUOTE(...))N
  • %SYSFUNC(NLITERAL(...))

 

On the other hand, as an example, the expressions below that are equal to "..."n or '...'n, do not cause any problems:

  • "..."n
  • '...'n
  • %SYSFUNC(QUOTE(...))n
  • %SYSFUNC(CATQ(A2N, ...))
  • %SYSFUNC(CATQ(A1N, ...))

 

The three dots above (...) can be replaced by, for example,

Valid.SPSS.variable.name

or

Leeftijd ** 2%STR(,) (of Age ** 2).

 

The reason why I used a SAS name literal that does not follow the default (V7) SAS naming conventions, is not because I would be in the habit of using such SAS variable names.

 

However, I have several SAS data sets, each containing hundreds of variables whose names also contain one or more dots.

These data sets come from an external organisation and were originally SPSS data files. The names of all variables in these data sets are all valid SPSS variable names. The fact of the matter is that a valid SPSS variable name may contain one or more dots, unlike a SAS variable name following the default (V7) SAS naming conventions. During the conversion of those SPSS data files to SAS data sets, the original SPSS variable names remained unchanged.

See

https://documentation.sas.com/doc/en/lrcon/9.4/p18cdcs4v5wd2dn1q0x296d3qek6.htm#p0w2dq3equv4b3n1m81y...

and

https://ezspss.com/rules-for-naming-variables-in-spss.

 

I posted the crashing SAS code with the SAS name literal just to easily illustrate the essence of the problem I faced when using those data sets converted from SPSS to SAS.

 

Kind regards,

 

Peter

 
 
 
Amir
PROC Star

Hi,

 

Your reported findings are interesting. For clarification are you looking for an explanation for the differences you are seeing or are you just sharing your findings?

 

If you want to see if the community has an explanation then I would advise you create a new question, including a link to this original thread. In your new question I would advise you post your logs showing the code with the error messages and the working code with the note messages, so people can see the difference and try to replicate the issue. If this does not help then you might have to open a ticket with SAS Technical Support.

 

HTH.

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

You need to provide some context where you get ERRORs.

I cannot replicate any errors.

1    options validvarname=any;
2    data test;
3    "..."N=1;
4    '...'N=2;
5    %SYSFUNC(QUOTE(...))N=3;
6    %SYSFUNC(NLITERAL(...))=4;
7    run;

NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

Since you seem convinced that the case of the letter N is an issue then I suspect you tried to compare strings and not use the text as variable names.  A lowercase N will not match an uppercase N in a string comparison.

 

To convert an nliteral string to the actual variable name (in all of its messiness) use the DEQUOTE() function.

27   data test;
28     name1="'A.b.c'n";
29     name2='"a.B.C"N';
30     if upcase(name1) ne upcase(name2) then put name1= 'and ' name2= 'are NOT the same strings.';
31     if upcase(dequote(name1)) = upcase(dequote(name2)) then
32       put name1= 'and ' name2= 'refer to the same variable';
33   run;

name1='A.b.c'n and name2="a.B.C"N are NOT the same strings.
name1='A.b.c'n and name2="a.B.C"N refer to the same variable
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
Peter_Boonants
Fluorite | Level 6

Hi Tom and Amir,

 

Thank you very much for your replies.

 

I found it interesting to share my findings, but I would also like to gain insight into the cause of the errors.

 

Although I have a problem in a PROC TABULATE with "..."N or '...'N, this is apparently not the case in a data step. In particular, I also get no errors when launching Tom's data steps above. So, "..."N and '...'N apparently work fine in a data step.

 

In the hope that this will provide further clarity to the issue for some readers, I have provided new illustrative SAS code.


Following Amir's advice, I have embedded the new illustrative SAS code and its corresponding log in a new question: PROC TABULATE TABLE statement using a SAS name literal variable name with ending capital N crashes.

 

Thanks in advance.

 

Kind regards,

 

Peter

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2105 views
  • 8 likes
  • 3 in conversation