DATA Step, Macro, Functions and more

Recoding into new vars from a character string

Regular Contributor
Posts: 202

Recoding into new vars from a character string

I have a variable ("medical") with character values that are various medical conditions. For example, in one observation "medical"=

"Arthritis/Joint Pain,Chronic Pain,Depression,Insomnia or other sleep disorder,Nausea"


I want to create a new variable for each medical condition, and have SAS input a "1" whenever that medical condition is among the values in the variable. Using the observation above, the desired output would be:








When I used the code below, the syntax executes correctly, but ONLY if the medical condition (e.g. Anxiety) is the ONLY value in the variable. In other words, the syntax only works correct when "medical"="anxiety", but not when "medical"="Anxiety,Depression,Chronic Pain"


	IF medical IN("Anxiety") THEN anxiety=1;
	ELSE IF medical IN("Arthritis/Joint Pain") THEN arthritis=1;
	ELSE IF medical IN("Chronic Pain") THEN chron_pn=1;
	ELSE IF medical IN("Depression") THEN depression=1;
	ELSE IF medical IN("Epilepsy or other seizure disorder") THEN seizures=1;
	ELSE IF medical IN("Nausea") THEN nausea=1;
	ELSE IF medical IN("PTSD") THEN ptsd=1;
	ELSE IF medical IN("Other reasons (please describe)") THEN other=1;
	ELSE IF medical IN("Allergies or Asthma") THEN allergies=1;
	ELSE IF medical IN("Insomnia or other sleep disorder") THEN sleep=1;
	ELSE IF medical IN("Migraine, cluster, tension headaches (which kind)") THEN ha=1;
	ELSE IF medical IN("COPD / other lung condition (please specify)") THEN resp=1;
	ELSE IF medical IN("Alzheimer's Disease") THEN alz_dz=1;
	ELSE IF medical IN("Multiple Sclerosis") THEN ms=1;
	ELSE IF medical IN("Parkinson's Disease") THEN pd=1;
	ELSE IF medical IN("Cancer (which kind)") THEN cancer=1;

Thanks for the assistance!

Posts: 1,580

Re: Recoding into new vars from a character string

Ideally it is better to avoid tons of if then statments and use proc format aka user defined format. Well, You could have tried select when otherwise syntax to say the least. Anyway since you have coded so much at length, change all your conditions using find function for a quick and easy/lazy fix.


for example:

if find(medical, "Anxiety")>0 then Anxiety=1;

Do the same for all the if conditions. HTH 

Super User
Posts: 3,857

Re: Recoding into new vars from a character string

Removing the ELSEs from your IF statements should give you the answer you want. But as @novinosrin has said there are better ways to dealing with a large number of IF conditions, including the use of a lookup format.

Super User
Posts: 6,632

Re: Recoding into new vars from a character string

[ Edited ]

This will require incorporating both suggestions.  Switch to FIND (or to INDEX), and remove the word ELSE.

Super User
Posts: 23,293

Re: Recoding into new vars from a character string

I would make a change to your process. 


1. Use SCAN() to separate each item in the list and create a long table, rather than a wide table. 

2. Map each diagnosis to a shorter string/variable name using PROC FORMAT 

3. Transpose using PROC TRANSPOSE to your desired format. 


I would also suggest using a common prefix in front of the diagnosis such as DIAG_. 

This will make it easier to reference all variables at once either in a short cut list or some similar functionality. 

If names aren't that important you could also consider just using DIAG1-DIAG<n> and leave the diagnosis as the labels. 


Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation