BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jo_rod
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
DanielSantos
Barite | Level 11

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

View solution in original post

10 REPLIES 10
art297
Opal | Level 21

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

 

jo_rod
Obsidian | Level 7

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.

Reeza
Super User

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 😉

ballardw
Super User

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).

 

ballardw
Super User

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.

Shmuel
Garnet | Level 18

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;
Astounding
PROC Star

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.

DanielSantos
Barite | Level 11

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

jo_rod
Obsidian | Level 7

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).   

art297
Opal | Level 21

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

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2022 views
  • 1 like
  • 7 in conversation