I came across the following line in one of the SAS program which I'm finding hard to understand. Any help?
if propcase(name) then name=name2;
Well, that is one of the most bizarre uses of PROPCASE (and bizarre uses of an IF statement) that I have ever seen. If you inherited this code, are there comments about what this line is doing?
Anyway, the text between IF and THEN
propcase(name)
is evaluated as true (a value of 1) or false (a value of 0) ... so a value of 0 is false, and any other non-missing value is true. So this brings up the question, what are some typical values of NAME?
If NAME='George', then propcase('George') is 'George', which is not a number, and so the IF statement is false.
If NAME='123' then propcase('123') is 123 and so the IF statement is true.
If you want, you can play with this bizarre usage of PROPCASE as follows, so you can see what different values of NAME produce
data class;
name='123';
z=propcase(name);
if propcase(name) then newvar=1;
else newvar=0;
run;
But I wouldn't waste any more time on this, as it is really a bad way to write code.
Well, that is one of the most bizarre uses of PROPCASE (and bizarre uses of an IF statement) that I have ever seen. If you inherited this code, are there comments about what this line is doing?
Anyway, the text between IF and THEN
propcase(name)
is evaluated as true (a value of 1) or false (a value of 0) ... so a value of 0 is false, and any other non-missing value is true. So this brings up the question, what are some typical values of NAME?
If NAME='George', then propcase('George') is 'George', which is not a number, and so the IF statement is false.
If NAME='123' then propcase('123') is 123 and so the IF statement is true.
If you want, you can play with this bizarre usage of PROPCASE as follows, so you can see what different values of NAME produce
data class;
name='123';
z=propcase(name);
if propcase(name) then newvar=1;
else newvar=0;
run;
But I wouldn't waste any more time on this, as it is really a bad way to write code.
If that's really the exact line of code, it's a mistake. And it should be generating an error (at least a bad NOTE) about invalid data, assuming name has character values that cannot be automatically converted into a numeric value:
data have ;
input name $1. name2 $1.;
if propcase(name) then name=name2;
cards ;
Q X
;
run ;
1 data have ; 2 input name $1. name2 $1.; 3 if propcase(name) then name=name2; 4 cards ; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 3:6 NOTE: Invalid numeric data, 'Q' , at line 3 column 6. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+-- 5 Q X name=Q name2= _ERROR_=1 _N_=1 NOTE: The data set WORK.HAVE has 1 observations and 2 variables. 6 ; 7 run ;
Propcase() doesn't evaluate if a string is propcase or not but it changes a string to propcase. For this reason using the propcase() function in an IF condition is just wrong.
I've been actually "surprised" that you even can use propcase() this way ...but then it's a function so why wouldn't SAS allow it even though it's "nonsense".
Below test shows you that propcase() always FALSE if used this way so it's no good at all.
data have;
name='ALFRED'; output;
name='alfred'; output;
name='Alfred'; output;
run;
data test;
set have;
if propcase(name) then bool=1;
else bool=0;
run;
proc print data=test;
run;
If you do have a need to test for the casing of a string then let us know and we can provide a correct coding solution.
@Patrick wrote:
Below test shows you that propcase() always FALSE if used this way so it's no good at all.
The only exception being if the NAME is a numeric code. This would not always return FALSE. But it would still be no good at all. : )
1 data have;
2 name='1'; output;
3 name='0'; output;
4 name='5'; output;
5 name='.'; output;
6 run;
NOTE: The data set WORK.HAVE has 4 observations and 1 variables.
7
8 data test;
9 set have;
10 if propcase(name) then bool=1;
11 else bool=0;
12
13 put (name bool)(=) ;
14 run;
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
10:6
name=1 bool=1
name=0 bool=0
name=5 bool=1
name=. bool=0
NOTE: There were 4 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.TEST has 4 observations and 2 variables
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.