BookmarkSubscribeRSS Feed
Boon_Noob
Calcite | Level 5
PersonAll the Diagnosis codes for this Person
11,2,4
22,3,4
35,6,7
41,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:

IDDiagnosis codeNew_Var
11,2,41
22,3,41
35,6,70
41,3,41
5 REPLIES 5
Tom
Super User Tom
Super User

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;
Reeza
Super User
FINDW() or restructure your data to a more logical structure that has each value in it's own column or mulitple rows per ID.

Boon_Noob
Calcite | Level 5

Thanks for the reply! If I have multiple rows per ID, how should I code it?

novinosrin
Tourmaline | Level 20
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;
ballardw
Super User

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.

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
  • 5 replies
  • 2025 views
  • 2 likes
  • 5 in conversation