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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Reeza
Super User

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.

data_null__
Jade | Level 19

Are you sure?

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


x=1
Reeza
Super User

Thanks Tom and John for the corrections.

Only leading blanks are an issue.

Mirisage
Obsidian | Level 7

H Tom, Reeza and Data_null,

Many thanks to each one of you for this contribution.

Regards

Miris

Tom
Super User Tom
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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