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
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;
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.
Are you sure?
Thanks Tom and John for the corrections.
Only leading blanks are an issue.
H Tom, Reeza and Data_null,
Many thanks to each one of you for this contribution.
Regards
Miris
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.