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

Hello programmers,

 

I have  my variables like this: heart1, heart2, heart3, and each of these variables have responses  coded as 1(most of the time), 2 (some of the time), 3 (rarely or never)

 

I want to create 3 new dichotomous (0,1) variables named heartvar1-heartvar3 which corresponds to heart1-heart3 such 

that heartvar1=1 if heart1=1 and heartvar1=0 otherwise, if heart1 is not missing. I want to also do this for heart2 and heart3.

 

 

I know i can use an array and a do over statement. Code is shown below

data one; set have;
array heart1-heart3;
array heartvar1-heartvar3;
do over ya heart1-heart3;
  if heart1=1 and heart1 ne . then heartvar1=1;
      else heartvar1=0; 
   if heart2= 1 and heart2 ne . then heartvar2= 1;
     else heartvar2=0;
      if heart3 = 1 and heart3 ne . then heartvar3= 1;
        else heartvar3=0;
  end;
  end;
  end;

 My code did not run well and i was wondering what could be wrong.

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Then main trouble is you did not create names for your arrays.

array heart heart1-heart3;
array heartvar heartvar1-heartvar3;

Now that you have the arrays defined you can use them.

You can use DO OVER.  SAS has removed it from the documentation, but it still works.  

data one; 
  set have;
  array heart heart1-heart3;
  array heartvar heartvar1-heartvar3;
  do over heart;
    if heart in (0 1) then heartvar = 1;
    else if heart =2 then heartvar=0;
    else heartvar=.;
  end;
run;

You can also use indexes into the arrays. Which is documented.

  do i=1 to dim(heart);
    if heart[i] in (0 1) then heartvar[i] = 1;
    else if heart[i] =2 then heartvar[i]=0;
    else heartvar[i]=.;
  end;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Then main trouble is you did not create names for your arrays.

array heart heart1-heart3;
array heartvar heartvar1-heartvar3;

Now that you have the arrays defined you can use them.

You can use DO OVER.  SAS has removed it from the documentation, but it still works.  

data one; 
  set have;
  array heart heart1-heart3;
  array heartvar heartvar1-heartvar3;
  do over heart;
    if heart in (0 1) then heartvar = 1;
    else if heart =2 then heartvar=0;
    else heartvar=.;
  end;
run;

You can also use indexes into the arrays. Which is documented.

  do i=1 to dim(heart);
    if heart[i] in (0 1) then heartvar[i] = 1;
    else if heart[i] =2 then heartvar[i]=0;
    else heartvar[i]=.;
  end;
ChuksManuel
Pyrite | Level 9

Thank you that was helpful

Kurt_Bremser
Super User

Read the log. For every ERROR or WARNING reported, read the documentation of the respective statement, and correct it. Do that top-down, because earlier ERRORs and WARNINGs are often the cause for later problems, and you may find that fixing the first problem does away all the others. Or that only fixing one ERROR enables SAS to find the following ones.

ScottBass
Rhodochrosite | Level 12

 

if heart1=1 and heart1 ne .

The and portion is redundant.  If heart1=1 then by definition it is not missing.

 

 

This is like saying:

 

 

if foo=1 and foo ne 2

 

A boolean statement (i.e. an if statement) will return 0 (false) or 1 (true).  So:

 

  if heart1=1 and heart1 ne . then heartvar1=1;
      else heartvar1=0; 
   if heart2= 1 and heart2 ne . then heartvar2= 1;
     else heartvar2=0;
      if heart3 = 1 and heart3 ne . then heartvar3= 1;
        else heartvar3=0;

 

can be abbreviated/simplified to:

 

heartvar1 = (heart=1);
heartvar2 = (heart=2);
heartvar3 = (heart=3);

Which using your arrays becomes:

 

array h{*} heart1-heart3;
array hv{*} heartvar1-heartvar3;
do i=1 to dim(h);
   hv{i}=(h{i}=1);
end;
drop i;

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
ChuksManuel
Pyrite | Level 9

Thank you!

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