Hi to everyone,
When I try to make a dataset, I would like to filter a variable as:
data new;
set old;
if sex='F';
run;
but, I dunno what happens, sometime sas dont read it properly, and i need to use:
if index(sex,'F')>0
and then it works.
Anyone can explain me why it happens with the first sentence, and why it works with the second one?
Thanks.,
J.
Trailing blanks are NOT the problem. SAS stores all character variables as fixed length strings. It pads the string with blanks to fill. If when you print the value it looks like F, but the test sex='F' does not work then the variable either contains a leading spaces or some other non-printable character. Print it with the $HEX format to see the character codes.
Values less than '20'x (space) or greater than '7F' (tilda) are probably what is causeing your problem.
24 data _null_;
25 length sex $5;
26 do sex = 'F',' F','F ';
27 put sex= ' ' sex $hex.;
28 end;
29 run;
sex=F 4620202020
sex=F 2046202020
sex=F 4620202020
Because you probably have embedded spaces somewhere in the text, ie your text is actually, " F" or "F "
Hexadecimal also rings a bell..
Sex either has leading spaces or some other character in there in addition to the letter F. So when you scan for the letter F it matches, but the value is not just the letter 'F' . Values could be ' F' or 'Female'. You might have a tab or carriage return character in there. You also might want to use UPCASE function to find the one's with lowercase f.
IF INDEX(UPCASE(SEX),'F') ;
Thanks. I understand it, but there are some way to know how many trailing blanks contains this variable?...or always you recommend index(upcase(sex),'F')
to avoid this problem?
Trailing blanks are NOT the problem. SAS stores all character variables as fixed length strings. It pads the string with blanks to fill. If when you print the value it looks like F, but the test sex='F' does not work then the variable either contains a leading spaces or some other non-printable character. Print it with the $HEX format to see the character codes.
Values less than '20'x (space) or greater than '7F' (tilda) are probably what is causeing your problem.
24 data _null_;
25 length sex $5;
26 do sex = 'F',' F','F ';
27 put sex= ' ' sex $hex.;
28 end;
29 run;
sex=F 4620202020
sex=F 2046202020
sex=F 4620202020
You could also try:
data new;
set old;
if strip(upcase(sex))='F';
run;
good idea art297!, it will remove the trailing blanks or leading blanks could contain this variable.
Thanks.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.