## Condensing if then statement

Solved
Occasional Contributor
Posts: 11

# Condensing if then statement

I've been having trouble condensing this very long "if then else" statement so that I can creat a separate variable for each level of the disease. I have to do this several times, so I was hoping someone would have an idea of how to fix this. Here is an example:

if lab=1 & disease=1 & aware=1 then m_aware1=1;
else if lab=1 & disease=1 & aware=0 then m_aware1=0;

if lab=1 & disease=2 & aware=1 then m_aware2=1;

else if lab=1 & disease=2 & aware=0 then m_aware1=0;
....
if lab=1 & disease=5 & aware=1 then m_aware5=1;
else if lab=1 & disease=5 & aware=0 then m_aware5=0;

I've tried a couple things that didn't seem to work. One of them was I created separate variables for each disease level, then using an array  but that just changed the information under each disease level, rather than making a new variable (below).

data test;
set qn.data;

array mic (5) disease1 -- disease5;
do i = 1 to 5;
if lab=1 then do;
if aware=1 then mic(i) = 1;
else if aware=0 then mic(i) = 0;
end;
end;
run;

Accepted Solutions
Solution
‎01-19-2017 11:17 AM
Super Contributor
Posts: 474

## Re: Condensing if then statement

Hi.

Assuming m_awarecan hold only 0 or 1 values:

``````data test;
set qn.data;
array m_aware[5] m_aware1 - m_aware5;
do i=1 to 5;
m_aware[i]=(lab eq 1 and disease eq i) and aware;
end;
run;``````

Hope it helps.

Daniel Santos @ www.cgd.pt

All Replies
PROC Star
Posts: 8,164

## Re: Condensing if then statement

Your code with the loop and array should have created new variables UNLESS those variables already existed before you ran your code. It would help if you could post either your data or a subset of your data.

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 11

## Re: Condensing if then statement

The variable did exist before. When I tried to re-create one in the same format SAS returned an error message about me needing to used a defined variable.

I would put a snipbit of the data, but it's pretty big and all over the place.

Super User
Posts: 23,754

## Re: Condensing if then statement

We don't need all of your data, probably only the variables involved. And it doesn't have to be real, it can be made up.

It really does look like you're calculating indicator variables and I partly wonder why...

At any rate, you'll have to post sample data to help further. Yes, it takes time, but answering your questions also take time

Super User
Posts: 13,563

## Re: Condensing if then statement

Post code and error message from the log into a code box, use the {i} icon.

Subset the data to a few records and only the variable you are using (keep statement and obs=10 or 15 or so).

Super User
Posts: 13,563

## Re: Condensing if then statement

These sure look like

if lab=1 & disease=2  then m_aware2=aware;

It often helps to post a few example records from your input data set. Instructions at https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... show how to create datastep code from a dataset. You can paste the text of the data step into a code window opened with the forum {i} icon or attach as a text file. You only need to include the variables of concern and enough examples to demostrate the issues. Desired output for the provided example input is very helpful.

Posts: 1,837

## Re: Condensing if then statement

[ Edited ]

In such case where I need to duplicate a code several times with slight change I prefer use a macro:

``````%macro check(dis);
if lab=1 & disease=&dis then m_aware&dis=aware;  /* aware equal 1 or 0 */
%mend check;

data want;
set have;
...
%check(1);
%check(2);
...
%check(5);
....
run;``````
Super User
Posts: 6,781

## Re: Condensing if then statement

It's logic, more than any sort of trick.  But this would be shorter, if it actually works:

if lab=1 then do;

if disease=1 then m_aware1=aware;

else if disease=2 then m_aware2=aware;

else if disease=3 then m_aware3=aware;

else if disease=4 then m_aware4=aware;

else if disease=5 then m_aware5=aware;

end;

When I say "if it actually works", that depends on whether AWARE is always 0 or 1, vs. it might take on other values.

Solution
‎01-19-2017 11:17 AM
Super Contributor
Posts: 474

## Re: Condensing if then statement

Hi.

Assuming m_awarecan hold only 0 or 1 values:

``````data test;
set qn.data;
array m_aware[5] m_aware1 - m_aware5;
do i=1 to 5;
m_aware[i]=(lab eq 1 and disease eq i) and aware;
end;
run;``````

Hope it helps.

Daniel Santos @ www.cgd.pt

Occasional Contributor
Posts: 11

## Re: Condensing if then statement

I've attached a small example of what the data looks like.

My intent is to create a new variables for everyone who had a lab result of '1' and then to dichotomize based on whether they were aware or not aware. Five variables are being created because I'm applying that to each level of the disease (which are 5 levels).

PROC Star
Posts: 8,164

## Re: Condensing if then statement

You didn't provide an example of what you want the resulting file to look like, so one can only guess. Here is my guess:

```data have;
input Obs ID disease aware lab;
cards;
1    2      .      0    .
2    5      1      1    .
3    7      .      0    .
4    10     .      .    .
5    12     2      0    1
6    13     2      0    1
7    14     .      0    .
8    15     .      1    .
9    16     3      0    1
10   24     .      0    .
11   25     .      1    .
12   29     2      0    2
13   33     .      0    .
14   34     .      1    .
15   45     .      0    .
16   46     5      1    1
17   55     .      0    .
18   56     .      0    .
19   57     4      0    2
20   59     .      0    .
;

data want;
set have;
array m_aware[5];
if lab eq 1 and not missing(aware) then
m_aware[disease]=ifn(aware eq 1,1,0);
run;
```

HTH,

Art, CEO, AnalystFinder.com

☑ This topic is solved.