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

Hello, 

 

I am trying to create a variable based on the responses of the two other variables in my dataset. For example, I want to create a new dichotomized var (0=no; 1=yes) called new_var using var1 and var2. If one of the responses is yes, or both of them are yes, then it should be categorized as yes. Ohterwise, it should be no.

 

I tried If-then-else-if statement, but it didnt work. Any suggestion would be greatly appreciated!

 

Thanks.  

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I would do something like:

data want;
  length new_var $5;
  set have;
  new_var="No";
if var1="Yes" or var2="Yes" then new_var="Yes"; run;

Seems a bit of a basic question though, are you sure this is what your asking?  Could shrink it too:

data want;
  length new_var $5;
  set have;
  new_var=ifc(var1="Yes" or var2="Yes","Yes","No");
run;

 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I would do something like:

data want;
  length new_var $5;
  set have;
  new_var="No";
if var1="Yes" or var2="Yes" then new_var="Yes"; run;

Seems a bit of a basic question though, are you sure this is what your asking?  Could shrink it too:

data want;
  length new_var $5;
  set have;
  new_var=ifc(var1="Yes" or var2="Yes","Yes","No");
run;

 

jhs2171
Obsidian | Level 7

Yes! Thank you! I had tried something very similar to what you suggested below, but it didnt' work. Thanks again!

chrej5am
Quartz | Level 8

If you have it coded like numbers>

data aaa;
input var1 var2 ;
datalines;
0 0
0 1
1 0
1 1
;
run;

data aaa;
set aaa;
if var1 or var2 then result=1;
else result=0;
run;

 

If you have it coded in strings be careful with comparing longer strings and upper lower cases

 

data bbb;
input var1 $3. var2 $3. ;
datalines;
No No
No Yes
YesNo
YesYes
;
run;

data bbb;
set bbb;
if lowcase(strip(var1))='yes' or lowcase(strip(var2))='yes' then result=1;
else result=0;
run;

 

Otherwise the logic is quite simple...

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 1980 views
  • 0 likes
  • 3 in conversation