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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

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

11 REPLIES 11
LinusH
Tourmaline | Level 20

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
Ksharp
Super User

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

MikeZdeb
Rhodochrosite | Level 12

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;

Ksharp
Super User

So long , Mike !

When are you going to China ?

Xia Keshan

Reeza
Super User

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;

munitech4u
Quartz | Level 8

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.

Astounding
PROC Star

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.

ballardw
Super User

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.

munitech4u
Quartz | Level 8

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

MikeZdeb
Rhodochrosite | Level 12

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;

munitech4u
Quartz | Level 8

Thanks for the multiply approach instead of if.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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