New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

Hi,

 

Sample data and code:

data sample_values;
  input letter $;
datalines;
A
B
C
run;

data lower_values;
  set sample_values;
  
  x = %lowcase(letter);
  y = lowcase(letter);
run;

proc print data=lower_values;
run;

The result:

Obs	letter	x	y
1	A	    A	a
2	B	    B	b
3	C	    C	c

Question 1:

For the first observation, the %lowcase didn't work, Is it because the %lowcase executed at compile time and the value for LETTER is still missing? 

 

Question 2:
What happened during the 2nd iteration for x?

x = %lowcase(letter);

If %lowcase is executed only once, what would the above line be equivalent to in the 2nd iteration?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

%LOWCASE should be used only on macro variables or the results of a macro expression. Example:

 

%let letter = A;
%put %lowcase(&letter);

 

LOWCASE should only be used on data set variables, or an expression involving data set variables. Example:

 

data fake;
    letter='A';
    letter2=lowcase(letter);
run;

 

If you use %LOWCASE when you should use LOWCASE, you may not get an error, but you may get unexpected results. So don't mix and match macro functions when you should be using normal functions, and vice versa.

 

 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

%LOWCASE should be used only on macro variables or the results of a macro expression. Example:

 

%let letter = A;
%put %lowcase(&letter);

 

LOWCASE should only be used on data set variables, or an expression involving data set variables. Example:

 

data fake;
    letter='A';
    letter2=lowcase(letter);
run;

 

If you use %LOWCASE when you should use LOWCASE, you may not get an error, but you may get unexpected results. So don't mix and match macro functions when you should be using normal functions, and vice versa.

 

 

--
Paige Miller
quickbluefish
Barite | Level 11

Yes, I'll just add one bit to what @PaigeMiller said, which is that the only place you'd sensibly use either one and expect to get the same results is if the argument is a string literal as opposed to a variable name.

data test;
x=%lowcase("Here is a STRING");
y=lowcase("Here is a STRING");
run;
PaigeMiller
Diamond | Level 26

@quickbluefish wrote:

Yes, I'll just add one bit to what @PaigeMiller said, which is that the only place you'd sensibly use either one and expect to get the same results is if the argument is a string literal as opposed to a variable name.

data test;
x=%lowcase("Here is a STRING");
y=lowcase("Here is a STRING");
run;

But I said you should not  use %LOWCASE with data step variables or data step expressions. Yes, it works the way you said for this particular string, but don't use %LOWCASE here, it is simply a very bad habit to get into, as there are many cases where it will not produce the same answer.

--
Paige Miller
quickbluefish
Barite | Level 11

It's a contrived example for illustrating a point, of course - basically, so that someone will understand the difference (or occasionally lack thereof) between the two.  Here's another:

** in SAS Studio, this runs in about 1 minute 7 seconds: ;
data test;
do i=1 to 100000000;
    x='frog';
    foundit=(index(lowcase('This is a very long string that 
    contains all sorts of text including FROG and Cats'),x));
    output;
end;
run;

** ...whereas the same thing, using %lowcase, runs in about 8 seconds ;
data test;
do i=1 to 100000000;
    x='frog';
    foundit=(index(%lowcase('This is a very long string that 
    contains all sorts of text including FROG and Cats'),x));
    output;
end;
run;
Tom
Super User Tom
Super User

Actually %LOWCASE() executed BEFORE compile time.  That is because it is not part of the SAS language, but is instead part of the SAS Macro Language.  The  macro processor pre-processes the text of your program and the results are then sent on to SAS itself to evaluate and run.

 

So this step 

data lower_values;
  set sample_values;
  x = %lowcase(LETTER);
  y = lowcase(letter);
run;

will be converted by the macro pre-processor into this step

data lower_values;
  set sample_values;
  x = letter;
  y = lowcase(letter);
run;

Which will then be compiled and run by SAS.

 

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!

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
  • 5 replies
  • 646 views
  • 8 likes
  • 4 in conversation