BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

6 REPLIES 6
Reeza
Super User

Because you probably have embedded spaces somewhere in the text, ie your text is actually, "  F" or "F  "

Hexadecimal also rings a bell..

Tom
Super User Tom
Super User

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') ;

michtka
Fluorite | Level 6

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?

Tom
Super User Tom
Super User

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

art297
Opal | Level 21

You could also try:

data new;

set old;

if strip(upcase(sex))='F';

run;

michtka
Fluorite | Level 6

good idea art297!, it will remove the trailing blanks or leading blanks could contain this variable.

Thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2168 views
  • 6 likes
  • 4 in conversation