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

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Dinurik
Fluorite | Level 6

Thank you, PaigeMiller! I tried it on my data and it worked. 

 

 

Reeza
Super User

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!

 


 

Dinurik
Fluorite | Level 6

I tried it on my data and it didn't work. SAS says:  Array subscript out of range at line "cond(i) = 1;"

Reeza
Super User
Is it possible to not have a 1 in any row? Otherwise that should work given your problem statement. You can add an IF condition to enter the loop only if the index is between 1 and 20.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 5 replies
  • 529 views
  • 0 likes
  • 3 in conversation