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;

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
  • 5 replies
  • 4458 views
  • 7 likes
  • 4 in conversation