Help using Base SAS procedures

Nested do loop in sas

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Nested do loop in sas

This is what i need to do on my dataset:

I have list of variables: p1-p9 and s1-s9. I want to create variables x1-x81 (9*9).

based on if p1=1 and s1=1 then x1=1; else x1 =0;

so on for all p1=1 with s1-s9 and so on for each p1-p9.

How to do this in sas. The manual if else takes so much time. I tried to use nested do loop by using 2 loops 1 for p1-p9 and another for s1-s9. but not working . I think array method will work.

But not sure how to let the nested loop use array of x1-x81.

Suggestions plz?

Thanks!


Accepted Solutions
Solution
‎06-12-2014 05:28 PM
Valued Guide
Posts: 765

Re: Nested do loop in sas

Posted in reply to munitech4u

hi ... maybe this is what you want ...

* make some fake data;

data have;

array det_lyrica_p(7);

array det_lyrica_s(7);

do i=1 to 5;

do j=1 to 7;

   det_lyrica_p(j) = ceil(5*ranuni(1234));

   det_lyrica_s(j) = ceil(5*ranuni(1234));

end;

   output;

end;

drop i j;

run;

proc print data=have;

run;

* add new variables x1-x49 based on the values of det_lyrica_p and det_lyrica_s;

data want;

array p(7)   det_lyrica_p1-det_lyrica_p7;

array s(7)   det_lyrica_s1-det_lyrica_s7;

* as suggested by KSharp ... array with two dimensions;

array x(7,7) x1-x49;

set have;

do i=1 to 7;

do j=1 to 7;

* logical 1s (condition true) and 0s (condition false)

   x(i,j) = p(i) eq 1 and s(j) eq 1;

end;

end;

drop i j;

run;

proc print data=want;

run;

View solution in original post


All Replies
Super User
Posts: 5,437

Re: Nested do loop in sas

Posted in reply to munitech4u

Why wouldn't an array work? Have you tried?

z=1;

    do i=1 to 9;

   do j=1 to 9;
   if p = s then x = 1;
   else x = 0;
   z+1;
   end;

    end;

Data never sleeps
Super User
Posts: 10,044

Re: Nested do loop in sas

Posted in reply to munitech4u

Or use two dimension array.

array _x{9,9}  x1-x81 ;

do i=1 to 9;

do j=1 to 9;
if p = s then x[i,j] = 1;
else x[i,j] = 0;
end;

    end;

Xia Keshan

Message was edited by: xia keshan

Valued Guide
Posts: 765

Re: Nested do loop in sas

Hi ... some other ideas ...

just make all x values 0 to start and just change the p=s x values to 1 ...

array _x{9,9}  x1-x81 (81*0);

do i=1 to 9;

do j=1 to 9;

  if p = s then _x[i,j] = 1;

end;

end;


or get rid if the IF ...


array _x{9,9}  x1-x81;

do i=1 to 9;

do j=1 to 9;

   _x(i,j) = p(i) eq s(j);

end;

end;

Super User
Posts: 10,044

Re: Nested do loop in sas

So long , Mike !

When are you going to China ?

Xia Keshan

Super User
Posts: 19,860

Re: Nested do loop in sas

Posted in reply to munitech4u

What are the other values of p1/s1. If it's only 1 and 0's you can multiply instead of if/condition.

I think that may be faster.

array _x{9,9}  x1-x81;

do i=1 to 9;

do j=1 to 9;

   _x(i,j) = p(i) * s(j);

end;

end;

Regular Contributor
Posts: 190

Re: Nested do loop in sas

Posted in reply to munitech4u

The condition was if p1=1 and s1=1, they may take other values for which i wanted 0.

This is what I tried unsuccessfully:

data temp;

array p det_lyrica_p1-det_lyrica_p7;

array s det_lyrica_s1-det_lyrica_s7;

array x det_lyrica_p8-det_lyrica_p56;

z=1;

    do i=1 to 7;

       do j=1 to 7;

           if p =1 and  s=1 then x = 1;

           else x = 0;

           z+1;

       end;

    end;

    run;

But it gave just 1 obs and made all p and s as . or missing and put all x=0. I need the variables x to be created for each obs level value.

Super User
Posts: 5,516

Re: Nested do loop in sas

Posted in reply to munitech4u

Do you have an existing data set with data for the P and S variables?  If so, there is nothing in your program that brings in that existing data set.

Do you want the program to create a separate observation for every possible combination of the P and S variables?  If so, you might have to tell us what values the variables are allowed to take.

Super User
Posts: 11,343

Re: Nested do loop in sas

Posted in reply to munitech4u

You need an OUTPUT statement just before the Z+1;

OR you need to have a SET statement to bring in existing data with the variables in the first two array statements. Without set they are all missing.

Regular Contributor
Posts: 190

Re: Nested do loop in sas

haha... what an error! thanks guys. Its working now, i had forgot to use the set statement.

Solution
‎06-12-2014 05:28 PM
Valued Guide
Posts: 765

Re: Nested do loop in sas

Posted in reply to munitech4u

hi ... maybe this is what you want ...

* make some fake data;

data have;

array det_lyrica_p(7);

array det_lyrica_s(7);

do i=1 to 5;

do j=1 to 7;

   det_lyrica_p(j) = ceil(5*ranuni(1234));

   det_lyrica_s(j) = ceil(5*ranuni(1234));

end;

   output;

end;

drop i j;

run;

proc print data=have;

run;

* add new variables x1-x49 based on the values of det_lyrica_p and det_lyrica_s;

data want;

array p(7)   det_lyrica_p1-det_lyrica_p7;

array s(7)   det_lyrica_s1-det_lyrica_s7;

* as suggested by KSharp ... array with two dimensions;

array x(7,7) x1-x49;

set have;

do i=1 to 7;

do j=1 to 7;

* logical 1s (condition true) and 0s (condition false)

   x(i,j) = p(i) eq 1 and s(j) eq 1;

end;

end;

drop i j;

run;

proc print data=want;

run;

Regular Contributor
Posts: 190

Re: Nested do loop in sas

Thanks for the multiply approach instead of if.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 6696 views
  • 14 likes
  • 7 in conversation