- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.