Help using Base SAS procedures

Index where if

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

Index where if

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.


Accepted Solutions
Solution
‎02-20-2012 07:56 PM
Super User
Super User
Posts: 6,502

Index where if

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


All Replies
Super User
Posts: 17,963

Index where if

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

Hexadecimal also rings a bell..

Super User
Super User
Posts: 6,502

Index where if

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

Super Contributor
Posts: 301

Index where if

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?

Solution
‎02-20-2012 07:56 PM
Super User
Super User
Posts: 6,502

Index where if

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

PROC Star
Posts: 7,366

Index where if

You could also try:

data new;

set old;

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

run;

Super Contributor
Posts: 301

Index where if

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

Thanks.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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