SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Dinurik
Fluorite | Level 6

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!

 

 

 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Reeza
Super User

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!

 

 

 

 


 

Astounding
PROC Star

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1081 views
  • 0 likes
  • 4 in conversation