SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

Hi all SAS Users,

 

Today I face a code:

proc print data=pg1.np_summary;
	var Type ParkName;
	where ParkName like '%Preserve%';
run;

And the result is

Phil_NZ_0-1617144399543.png

I have some concerns here:

1. I remember inside the single quotation, the word is sensitive, so how come it displays all uppercase in the result report? I know that the percentage symbol represents any character or blank, or else. But how do two percent symbols work in this case? And normally, to me, 'Preserve%' will display some names that should be longer than 'Preserve', so when there are some results like 'PRE' in the result, it is a bit surprising to me?

 

2. Why when I change the single quotation to double quotation, it displayed a warning? (We all know that single and double quotation can be used interchangeably, except we must use double quotation for macro variables)

42         proc print data=pg1.np_summary;
43         	var Type ParkName;
44         	*Add a WHERE statement;
45         	where ParkName like "'%Preserve%'";
WARNING: Apparent invocation of macro PRESERVE not resolved.
46         run;

Warmest regards and cheers,

 

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The % is a wildcard for the LIKE operator.  So LIKE '%Preserve%' will match any string that contains Preserve.  Note it will not match PREserve (unless it gets pushed into some remove database that support case insensitive comparison, like Teradata).

 

You answered the second question yourself.  The % character is a trigger to the macro processor (the people that defined SQL and how the LIKE operator works did not take into consideration that SAS uses the % character for a different meaning).  But the macro processor ignores strings that use single quotes as the encapsulating character.  But when you switch from '%Preserve%' to "%Preverse%" the first % looks like it is a call to some macro named PRESERVE.  The macro processor will ignore the second % because the character after it cannot be the start of a macro name or macro language keyword.

 

Note that "'%Perserve%'" has two problems.  First it is bounded by double quotes so the macro processor will try to find the %PRESERVE macro.  Second the search pattern now contains the single quotes.  So it will only match values of the variable that also have single quotes on the outside.  In particular the single quotes must be in the first position and the last possible position (based on the length of the variable) so that it is not followed by of the trailing spaces that SAS uses to pad the character variable values to their fixed maximum length. 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

The % is a wildcard for the LIKE operator.  So LIKE '%Preserve%' will match any string that contains Preserve.  Note it will not match PREserve (unless it gets pushed into some remove database that support case insensitive comparison, like Teradata).

 

You answered the second question yourself.  The % character is a trigger to the macro processor (the people that defined SQL and how the LIKE operator works did not take into consideration that SAS uses the % character for a different meaning).  But the macro processor ignores strings that use single quotes as the encapsulating character.  But when you switch from '%Preserve%' to "%Preverse%" the first % looks like it is a call to some macro named PRESERVE.  The macro processor will ignore the second % because the character after it cannot be the start of a macro name or macro language keyword.

 

Note that "'%Perserve%'" has two problems.  First it is bounded by double quotes so the macro processor will try to find the %PRESERVE macro.  Second the search pattern now contains the single quotes.  So it will only match values of the variable that also have single quotes on the outside.  In particular the single quotes must be in the first position and the last possible position (based on the length of the variable) so that it is not followed by of the trailing spaces that SAS uses to pad the character variable values to their fixed maximum length. 

Phil_NZ
Barite | Level 11

Hi @Tom 

Thank you for your explanation, I am wondering why the result from running the code as above contains these words, it seems to against your explanation.

Phil_NZ_0-1617147719835.png

Please correct me if I get you wrongly?

Thanks in advance.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
SASKiwi
PROC Star

Your program filters on ParkName not Type.

Tom
Super User Tom
Super User

Because it found the word Preserve.  Check out the green highlights.

image.png

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 5958 views
  • 3 likes
  • 3 in conversation