I am on SAS 9.4 and am trying to currently use an 'if' statement to create a new column based off another column's variable values.
I am trying to make this new column, 'have' have either a 'Y' or a 'N' based off what the variable value is from 'code'.
For example this is the code that I have
Code: OLD
Patient code
1 A
1 A2
1 A11
1 C
1 B34
1 F56
1 A19
1 M0
1 N9
and what I want is this:
Code: NEW
Patient code Have
1 A Y
1 A2 Y
1 A11 Y
1 C N
1 B34 N
1 F56 N
1 A19 Y
1 M0 N
1 N9 N
Here is the code that I am trying to use.
data new;
set old;
if code = A, A2, A11, A19 then have='Y'
end;
else have="N";
run;
Could anyone help?
Thanks!
if code = 'A' or code = 'A2' or code='A11' or code='A19' then have='Y';
Don't forget the semicolon at the end of the IF statement.
Or even simpler (slightly less coding)
if code in ('A','A2','A11','A19') then have='Y';
if code = 'A' or code = 'A2' or code='A11' or code='A19' then have='Y';
Don't forget the semicolon at the end of the IF statement.
Or even simpler (slightly less coding)
if code in ('A','A2','A11','A19') then have='Y';
Sir @PaigeMiller did you have your coffee ? why not IN operator?
I was trying to instruct the user in his attempt to code this problem, by modifying his code to something that works but still looks like what he was attempting. So he can learn proper syntax of the IF statement.
Or if it is appropriate, and you just want to find all the codes that begin with the letter A (which isn't specifically stated by @Kbug and may or may not be what he is attempting to do)
if code=:'A' then have='Y';
the colon indicates find all values of code that BEGIN WITH the quoted string, in this case 'A'
It worked, thank you!
Assuming you want the A series as 'Y'
data have;
input Patient code $;
cards;
1 A
1 A2
1 A11
1 C
1 B34
1 F56
1 A19
1 M0
1 N9
;
data want;
set have;
want=ifc(code=:'A','Y','N');
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.