- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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';
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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';
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sir @PaigeMiller did you have your coffee ? why not IN operator?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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'
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It worked, thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;