BookmarkSubscribeRSS Feed
momi
Obsidian | Level 7

 

Hello,

 

I have two questions about COMPARE function. 

 

1. I am looking at this link (https://go.documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p0iaa25pnhl59ln1v4yrympngpic....) and I am not clear about the definition of 'name literal.'  Does anyone have more examples that explain the term? 

 

n or N removes quotation marks from any argument that is a name literal and ignores the case of string-1 and string-2. A name literal is a name token that is expressed as a string within quotation marks, followed by the uppercase or lowercase letter n. Name literals enable you to use special characters (including blanks) that are not otherwise allowed in SAS data set or variable names. For COMPARE to recognize a string as a name literal, the first character must be a quotation mark.

 

2. Example 1 the link has the code below and one of the sample string comparison,

'$12'n $12

has three different cases to show the result of COMPARE.  I am not clear how the result with a modifier 'nl' (#14 in the table below)did not get the 0 result.

 

Thanks!

 

 

 

data test;
   infile datalines missover;
   input string1 $char8. string2 $char8. modifiers $char8.;
   result=compare(string1, string2, modifiers);
   datalines;
1234567812345678
123     abc
abc     abx
xyz     abcdef
aBc     abc
aBc     AbC     i
   abc  abc
   abc  abc     l
 abc       abx
 abc       abx  l 
ABC     'abc'n
ABC     'abc'n  n 
 '$12'n $12     n
 '$12'n $12     nl
 '$12'n $12     ln
;

proc print data=test;
run;

 

Results of Comparing Two Strings Using the COMPARE Function
Results of Comparing Two Strings Using the COMPARE Function
2 REPLIES 2
ballardw
Super User

@momi wrote:

 

Hello,

 

I have two questions about COMPARE function. 

 

1. I am looking at this link (https://go.documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p0iaa25pnhl59ln1v4yrympngpic....) and I am not clear about the definition of 'name literal.'  Does anyone have more examples that explain the term? 

 

n or N removes quotation marks from any argument that is a name literal and ignores the case of string-1 and string-2. A name literal is a name token that is expressed as a string within quotation marks, followed by the uppercase or lowercase letter n. Name literals enable you to use special characters (including blanks) that are not otherwise allowed in SAS data set or variable names. For COMPARE to recognize a string as a name literal, the first character must be a quotation mark.

 


SAS Variable names are generally restricted to starting with either an underscore character, _ , or a letter followed by underscore, letter or digits. No spaces, other punctuation characters allowed. When connection to other data bases that allow that sort of "name" for columns or variable SAS added the name literal as a way to allow users to code in SAS and address such things. The SAS option VALIDVARNAME has to be set to ANY to allow the use. The construct of name literal is a quoted string immediately followed by an n(upper or lowercase0 to indicate the string is intended to be a name literal value.

 

SAS uses a similar convention with dates, times and datetimes where quoted strings in certain formats followed immediately by d, dt or t tell SAS to use the value as a Date, Datetime or time respectively. Example: '05JAN2019'd will be treated as a SAS date value; "09:15"t is a time value.

 

 

 

 

momi
Obsidian | Level 7

Thank you.

 

So '$12'n means any string with 12 characters?

 

Thanks,

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 545 views
  • 0 likes
  • 2 in conversation