Person | All the Diagnosis codes for this Person |
1 | 1,2,4 |
2 | 2,3,4 |
3 | 5,6,7 |
4 | 1,2,3,4 |
If I have a dataset that looks like this, where a variable column contains multiple values per row. So one person can have multiple codes. Here I want to define a new variable that if a person's codes contains either 2 or 3, then new variable =1 otherwise new variable =0.
I want something like this:
ID | Diagnosis code | New_Var |
1 | 1,2,4 | 1 |
2 | 2,3,4 | 1 |
3 | 5,6,7 | 0 |
4 | 1,3,4 | 1 |
Assuming your dataset is named HAVE and your character variable with the list of codes is named DIAGNOSIS_CODE then use code like:
data want;
set have;
new_var = indexw(diagnosis_code,'2') or indexw(diagnosis_code,'3');
run;
Thanks for the reply! If I have multiple rows per ID, how should I code it?
data have;
input Person Diagnosis_codes :$10.;
cards;
1 1,2,4
2 2,3,4
3 5,6,7
4 1,2,3,4
;
data want;
set have;
New_Var=^^findc(Diagnosis_codes,'23');
run;
It helps to provide example data in the form of a data step so we have something to write code to test and at least your actual variable names.
Something like this should work for your specific example:
data want; set have; new_var = (index(diagnosiscodes,'2')>0 or index(diagnosiscodes,'3') > 0); run;
The index function returns a position number in the string searched. The first parameter is the string to search, the second is what to search for. If the value is not found then the function returns 0.
SAS will return a value of 1 for "True" and 0 for "False". So comparisons are a quick way to create 1/0 coded values.
You will find in the long run that placing multiple values into a single variable is awkward to code with and may result in very complex processes that do not extend well when new values are added.
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.