Hi SAS programmers,
I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.
So say we have a dataset
data miss;
input miss1-miss5;
cards;
1 1 1 1 1
1 0 1 1 1
1 1 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 1 1
;
run;
Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.
I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.
So far what I wrote is:
data miss2;
set miss1;
unatt_count = 0;
array idata(*) miss1-miss5;
array dd(*) d1-d5;
do i = 1 to dim(idata);
if idata(i) = 1
then dd(i) = unatt_count ;
else if idata(i) = 0
then dd(i) = unatt_count+1;
end;
run;
As expected SAS only returns values 1 and 0 for d1-d5, because unatt_count needs to be updated within a loop. I know my loop may be completely idiotic for the purpose (I am a novice to SAS programming), so I would really appreciate any advice!
Thanks!
@Dinurik wrote:
Hi SAS programmers,
I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.
So say we have a dataset
data miss;
input miss1-miss5;
cards;
1 1 1 1 1
1 0 1 1 1
1 1 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 1 1
;
run;
Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.
I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.
Show us the desired output from this sample data.
I think you're really looking for the first 1 in the series?
If so, WHICHN is all you need.
data miss;
input miss1-miss5;
first1 = whichn(1, of miss1-miss5);
cards;
1 1 1 1 1
1 0 1 1 1
1 1 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 1 1
;
run;
@Dinurik wrote:
Hi SAS programmers,
I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.
So say we have a dataset
data miss;
input miss1-miss5;
cards;
1 1 1 1 1
1 0 1 1 1
1 1 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 1 1
;
run;
Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.
I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.
So far what I wrote is:
data miss2;
set miss1;
unatt_count = 0;
array idata(*) miss1-miss5;
array dd(*) d1-d5;
do i = 1 to dim(idata);
if idata(i) = 1
then dd(i) = unatt_count ;
else if idata(i) = 0
then dd(i) = unatt_count+1;
end;
run;
As expected SAS only returns values 1 and 0 for d1-d5, because unatt_count needs to be updated within a loop. I know my loop may be completely idiotic for the purpose (I am a novice to SAS programming), so I would really appreciate any advice!
Thanks!
If I understand the question correctly:
data miss2;
set miss1;
array idata {5} miss1-miss5;
array dd {5} d1-d5;
d1=0;
unatt_count=0;
do i = 2 to dim(idata);
unatt_count + (idata{i-1} = 0);
if idata{i}=0 then dd{i} = unatt_count;
end;
run;
D1 must be 0, since there can't be any unattended meetings before meeting 1.
MISS5 never gets counted in the unattended count, since it can't be used as a prior meeting. If you want to adjust the final unattended count to examine MISS5 you could add a final statement to the DATA step:
if miss5=0 then unatt_count + 1;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.