Help using Base SAS procedures

if index (...) then gives correct reults but if IN (..) not?

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

if index (...) then gives correct reults but if IN (..) not?

Hi Sas Forum,

I Have the attahced k_1 data set.

I wanted to create a flag with the following condition.

if STATUS in("CURRENT") then Delq_Flag=0;

else Delq_Flag=1;

So, I used below code.

data k_3;

set k_1;

if STATUS in("CURRENT") then Delq_Flag=0;

else Delq_Flag=1;

run;

Q: But above code doesn't give correct results although the logic seems correct. But below code provides the intended resutls. I cannot understand what is happening. Coudl someone help me to understand.

data k_2;

set k_1;

if index(STATUS,"CURRENT") then Delq_Flag=0;

else Delq_Flag=1;

run;

Thanks

Miris

Attachment

Accepted Solutions
Solution
‎03-10-2014 11:35 AM
Super User
Super User
Posts: 6,500

Re: if index (...) then gives correct reults but if IN (..) not?

In SAS trailing blanks are meaningless, but leading blanks can cause these type of behavior.  Leading blanks can be hard to see are they normally do not appear when using PUT as it will trim them. You can see them better if you use the $QUOTE. format.

data k_2;

set k_1;

if index(STATUS,"CURRENT") and not STATUS in ("CURRENT") then put status $quote. ;

run;

View solution in original post


All Replies
Super User
Posts: 17,829

Re: if index (...) then gives correct reults but if IN (..) not?

Your dataset is protected, probably for good reason.

Run a proc freq on your variable of interest. Most likely it has trailing blanks or some other letters that cause it not to be an exact match.

ie

Current is not equal to "Current   " (3 extra spaces)

but the index function would return a match.

EDIT: Trailing blanks are not an issue, Leading Blanks would cause a mismatch.

Respected Advisor
Posts: 3,777

Re: if index (...) then gives correct reults but if IN (..) not?

Are you sure?

data _null_;
  
x = 'Current' eq 'Current   ';
  
put x=;
   run;


x=1
Super User
Posts: 17,829

Re: if index (...) then gives correct reults but if IN (..) not?

Thanks Tom and John for the corrections.

Only leading blanks are an issue.

Super Contributor
Posts: 338

Re: if index (...) then gives correct reults but if IN (..) not?

H Tom, Reeza and Data_null,

Many thanks to each one of you for this contribution.

Regards

Miris

Solution
‎03-10-2014 11:35 AM
Super User
Super User
Posts: 6,500

Re: if index (...) then gives correct reults but if IN (..) not?

In SAS trailing blanks are meaningless, but leading blanks can cause these type of behavior.  Leading blanks can be hard to see are they normally do not appear when using PUT as it will trim them. You can see them better if you use the $QUOTE. format.

data k_2;

set k_1;

if index(STATUS,"CURRENT") and not STATUS in ("CURRENT") then put status $quote. ;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 191 views
  • 7 likes
  • 4 in conversation