BookmarkSubscribeRSS Feed
_maldini_
Barite | Level 11

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:

 

arthritis=1

chron_pn=1

depression=1

insomnia=1

nausea=1

 

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!

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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 

SASKiwi
PROC Star

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.

Astounding
PROC Star

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

Reeza
Super User

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. 

 

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
  • 4 replies
  • 2102 views
  • 4 likes
  • 5 in conversation