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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
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
novinosrin
Tourmaline | Level 20

Sir @PaigeMiller  did you have your coffee ? why not IN operator?

PaigeMiller
Diamond | Level 26

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
PaigeMiller
Diamond | Level 26

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
Kbug
Obsidian | Level 7

It worked, thank you!

novinosrin
Tourmaline | Level 20

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 6 replies
  • 16888 views
  • 3 likes
  • 3 in conversation