Hi SAS Users,
I have been trying to resolve this problem, but keep failing, therefore I would greatly appreciate any help.
The data are binomial (whether a participant has a condition (1) or not(0)). There are 20 repeated measurements corresponding to 20 visits:
cond1 cond2 .... cond20
0 0 ... 0
0 1 ... 0
1 0 .... 0
0 0 ... 1
etc.
I need to create 20 variables based on cond1-cond20, so that if a person had a condition at visit 1 all visits after it would be marked as 1; if condition was present at visit 2, all visits after the second would be marked as 1, etc.
cond_new1 cond_new2 cond_new 3 .... cond_new20
0 0 0 ... 0
0 1 1... 1
1 1 1... 1
0 0 0 ... 1
My current code is:
data interim;
set old;
array old_cond(20) cond1 - cond20;
array new_cond(20) new_cond1 - new_cond20;
do i = 1 to 20;
new_cond(i) = old_cond(i);
end; drop i;
run;
data want;
set have;
array old_cond(19) cond1 - cond19;
array new_cond(20) new_cond1 - new_cond20;
do i = 1 to 19;
do j = 1 to 19;
if old_cond(i) = 1 then new_aids(i+j) = 1;
end; end;
run;
And the error message says that the array subscript is out of range.
I cannot find in any SAS resources how to update an indexing variable so that it moved from say visit 2 to visit 20 and changed the value from 0 to 1 until it reaches the last visit. I would be really grateful for any help.
Thanks!
If i=18 and j=18 then you have a subscript of i+j which is 36, which is out of range.
Since you didn't actually provide data, this is UNTESTED code
data want;
set have;
array cond(20) cond1 - cond20;
do i = 2 to 20;
if cond(i-1)=1 then cond(i)=1;
end;
drop i;
run;
If i=18 and j=18 then you have a subscript of i+j which is 36, which is out of range.
Since you didn't actually provide data, this is UNTESTED code
data want;
set have;
array cond(20) cond1 - cond20;
do i = 2 to 20;
if cond(i-1)=1 then cond(i)=1;
end;
drop i;
run;
Thank you, PaigeMiller! I tried it on my data and it worked.
Way too complicated, this will change in place with your old variables. If you don't want that you could create a new array of all values easily.
data want;
set have;
array cond(20);
index = whichn(1, of cond(*));
do i=index to dim(cond);
cond(i) = 1;
end;
run;
@Dinurik wrote:
Hi SAS Users,
I have been trying to resolve this problem, but keep failing, therefore I would greatly appreciate any help.
The data are binomial (whether a participant has a condition (1) or not(0)). There are 20 repeated measurements corresponding to 20 visits:
cond1 cond2 .... cond20
0 0 ... 0
0 1 ... 0
1 0 .... 0
0 0 ... 1
etc.
I need to create 20 variables based on cond1-cond20, so that if a person had a condition at visit 1 all visits after it would be marked as 1; if condition was present at visit 2, all visits after the second would be marked as 1, etc.
cond_new1 cond_new2 cond_new 3 .... cond_new20
0 0 0 ... 0
0 1 1... 1
1 1 1... 1
0 0 0 ... 1
My current code is:
data interim;
set old;
array old_cond(20) cond1 - cond20;
array new_cond(20) new_cond1 - new_cond20;
do i = 1 to 20;
new_cond(i) = old_cond(i);
end; drop i;
run;
data want;
set have;
array old_cond(19) cond1 - cond19;
array new_cond(20) new_cond1 - new_cond20;
do i = 1 to 19;
do j = 1 to 19;
if old_cond(i) = 1 then new_aids(i+j) = 1;
end; end;
run;
And the error message says that the array subscript is out of range.
I cannot find in any SAS resources how to update an indexing variable so that it moved from say visit 2 to visit 20 and changed the value from 0 to 1 until it reaches the last visit. I would be really grateful for any help.
Thanks!
I tried it on my data and it didn't work. SAS says: Array subscript out of range at line "cond(i) = 1;"
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 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.