turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Nested do loop in sas

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-12-2014 06:33 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 05:28 PM

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;**

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 07:15 AM

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

z=1; |

do i=1 to 9;

do j=1 to 9; | ||

if p = s | ||

else x | ||

z+1; | ||

end; |

end;

Data never sleeps

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 08:20 AM

Or use two dimension array.

array _x{9,9} x1-x81 ;

do i=1 to 9;

do j=1 to 9; | ||

if p = s | ||

else x[i,j] = 0; | ||

end; |

end;

Xia Keshan

Message was edited by: xia keshan

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

06-12-2014 09:26 AM

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**

**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;**

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MikeZdeb

06-12-2014 09:31 AM

So long , Mike !

When are you going to China ?

Xia Keshan

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 10:36 AM

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;**

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 10:50 AM

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*

else x

z+1;

end;

end;

run;

But it gave just 1 obs and made all p* and s as . or missing and put all x*

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 11:03 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 11:05 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

06-12-2014 11:12 AM

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

Solution

06-12-2014
05:28 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to munitech4u

06-12-2014 05:28 PM

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;**

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MikeZdeb

06-13-2014 02:05 AM

Thanks for the multiply approach instead of if.