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!
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;
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;
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
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;
So long , Mike !
When are you going to China ?
Xia Keshan
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;
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
else x
z+1;
end;
end;
run;
But it gave just 1 obs and made all p and s
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.
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.
haha... what an error! thanks guys. Its working now, i had forgot to use the set statement.
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;
Thanks for the multiply approach instead of if.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.