I need help creating a new binary variable from an existing data set. The variable name is deathcauses (Cerebral Vascular Disease Coronary Heart Disease, Cancer, Other, or Unknown). I want to create a new binary variable of 0 = heart-related causes and 1 = other causes. The data set is FraminghamD. Please, I need help with the correct SAS code. Thank you
if deathcauses in ('Vascular Disease','Coronary Heart Disease') then binary_variable=1;
else binary_variable=0;
if deathcauses in ('Vascular Disease','Coronary Heart Disease') then binary_variable=1;
else binary_variable=0;
A binary variable can be created using IF/THEN statements. However, without knowing your variable name it's hard to suggest the 'correct' SAS code.
An example would be the following, which creates a binary variable that shows if the sex of a person is male or female.
data want;
set sashelp.class;
if sex='F' then Female=1;
else Female=0;
run;
@oniume wrote:
I need help creating a new binary variable from an existing data set. The variable name is deathcauses (Cerebral Vascular Disease Coronary Heart Disease, Cancer, Other, or Unknown). I want to create a new binary variable of 0 = heart-related causes and 1 = other causes. The data set is FraminghamD. Please, I need help with the correct SAS code. Thank you
if you need to search character string for multiple strings then you can use multiple INDEX() or FIND() or else you can use single PRXMATCH() with multiple substring values listed.
data want;
set have;
if index(lowcase(charvar),'this') > 0 or
index(lowcase(charvar),'that') > 0 or
index(lowcase(charvar),'other') > 0 then found = 1;
else found=0;
run;
data want;
set have;
if prxmatch("m/this|that|other/oi",charvar) > 0 then found=1;
else found=0;
run;
Hi @oniume Welcome to the SAS forum and Good morning. Please post sample of data that you HAVE and the required output you WANT than just sentences for us to work with in the future.
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.