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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.