## help with sas code

Occasional Contributor
Posts: 5

# help with sas code

Hi,

Thank you in advance for any help people provide.

To give some background before I ask my question...

v1-v10 are the number of months between two dates consecutive dates.  There is one less v variable than the flag variable due to the v variable measuring the interval between dates.

flag1-flag11 are the variables that denote if a condition was meet for a given date.

I would like some help in figuring out some code that would give me the sum of the values of v1-v10 for any consecutive period or periods where flag1-flag11 are yes.  There could be more than one period of time per observation, and I would want all periods as long as consecutive flag values equal yes.  For example if there is a case where there are 3 periods of  consecutive 'yes' interupted by a  couple of 'no' dis[pused throughout the records, I am wanting something that provides the lenght of time for period 1 and then the length of time for period 2, and then the length of time for period 3.

Here is code I have written so far:

data want;

set have;

if flag1='yes' and flag2='yes' then do;

yt1=v1+v2; *used to track the time where flag=yes;

nt1=0;    *used to track the time where flag=no;

kg1='yy'; *used to track pairing for future use in the code;

prd1='y'; *used to track which period;

end;

if flag1='no' and flag2='no' then do;

yt1=0;

nt1=v1+v2;

kg1='nn';

prd1='y';

end;

if flag1='no' and flag2='yes' then do;

yt1=0;

nt1=0;

kg1='ny';

prd1='y';

end;

if flag1='yes' and flag2='no' then do;

yt1=0;

nt1=0;

kg1='yn';

prd1='y';

end;

if flag1='no' and flag2=' ' then do;

nt22=88888888; *denotes only one event and the event did not meet the condition;

kg='f';

prd1='only1';

end;

if flag1='yes' and flag2=' ' then do;

nt22=77777777;   *denotes only one event and the event meets the condition;

kg1='f';

prd1='only1';

end;

if flag2='yes' and flag3='yes' and kg1='yy' then do;

yt2=yt1+v2;

nt2=nt1+0

kg2='yy';

prd1='y';

end;

if flag2='yes' and flag3='yes' and kg1='ny' then do;

yt2=yt1+v2;

nt2=nt1+0

kg2='yy';

prd1='y';

end;

if flag2='no' and flag3='no' and kg1='nn' then do;

yt2=yt1+0;

nt2=nt2+v2;

kg2='nn';

prd1='y';

end;

if flag2='no' and flag3='no' and kg1='yn' then do;

yt2=yt1+0;

nt2=nt2+v2;

kg2='nn';

prd1='y';

end;

if flag2='no' and flag3='yes' and kg1='yn' then do;

yt2=yt1+0;

nt2=nt1+0;

kg2='ny';

prd1='y';

end;

if flag2='yes' and flag3='no' and kg1='ny' then do;

yt2=yt1+0;

nt2=nt1+0;

kg2='yn';

prd1='y';

end;

if flag2='no' and flag3=' ' and kg1='nn' then do;

nt22=nt1+nt2;

kg2='f';

end;

if flag2='no' and flag3=' ' and kg1='yn' then do;

nt22=nt1+0;

kg2='f';

end;

if flag2='yes' and flag3=' ' and kg1='yy' then do;

yt22=yt1+yt2;

kg1='f';

end;

run;

The idea is to write more code that looks at flag3 and flag4 following the same logic as the code above.  However, the code will become very long and also there will have to be more code to deal with records that have more than one period.  I am sure there is a better way but I do not know it.  I appreciate any help in figuring this out.

The sample data set below is not the full set.  The full set would be much too big to post.

The data look like this:

 Obs v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 flag1 flag2 flag3 flag4 flag5 flag6 flag7 flag8 flag9 flag10 flag11 1 2 3 6 6 3 3 . . . . yes yes yes yes yes yes yes 2 6 6 5 7 0 5 4 2 . . yes yes yes yes no no yes yes no 3 4 4 7 6 7 . . . . . yes yes yes yes yes yes 4 2 5 9 10 0 4 2 . . . yes yes yes yes yes yes yes yes 5 5 9 4 4 6 . . . . . no no no no no no 6 2 32 . . . . . . . . no no no 7 4 3 7 . . . . . . . yes no no no 8 7 9 5 10 . . . . . . yes yes no yes yes 9 2 0 1 3 2 . . . . . no no no no yes yes 10 3 3 9 5 4 5 . . . . yes yes yes yes yes yes no 11 13 6 . . . . . . . . no yes yes 12 5 0 7 5 4 6 5 . . . yes yes yes yes yes yes yes yes 13 14 . . . . . . . . . yes yes 14 7 1 . . . . . . . . no yes yes 15 2 2 1 3 4 3 . . . . no no no no no no no 16 5 7 5 0 . . . . . . yes yes yes no no 17 6 3 6 1 5 5 3 . . . yes yes yes yes yes yes yes yes 18 5 4 4 3 4 5 6 . . . yes yes yes yes yes yes yes no 19 3 3 3 3 3 3 3 4 3 3 yes yes yes yes yes yes yes yes yes yes yes 20 3 2 6 3 4 1 4 4 . . no yes yes yes yes yes yes yes yes
Super User
Posts: 23,683

## Re: help with sas code

Please post sample data as a data step and include what you want as output, I don't see that in your post.

I would likely recommend you transpose to a long format, calculate your new variables and then transpose it again.

Occasional Contributor
Posts: 5

## Re: help with sas code

Hi Reeza,

Sorry about not including wanted output.

The output I am wanting would look something like this (in reference to Obs 2)

prd_1=17

It is 17 because of (v1+v2+v3). v1, v2,v3 are the interval for which flag1-flag4 are yes.

Then because of flag4=no the first period is closed and a new period can begin provide two or more flag variabls in a row equal yes.

prd_2=4

It is 4 because of (v7). This is the interval that for which 2 flag variables in a row were yes (flag7 and flag8).

As for the posting of the data, I am confused by that request as that is in the post.  It is the table with the first row called "Obs" and the 2nd row called "v1", and so on.  From my end, when I look at the post, I am able to see the data.  Is that not visable from your end?

Hopefully my response to you clarifies things.

Thanks

Super User
Posts: 23,683

Super User
Posts: 23,683

## Re: help with sas code

Last question - do you know the number of flags you need ahead of time? I guess the mean is the number of variables?

Occasional Contributor
Posts: 5

## Re: help with sas code

Hi Rezza,

The number of flag variables is dependant on if a certain condition was meant.  Each record will have a varying number of flags because there will be a variable number of dates for each record (or person).  For my outcome of interest, I am wanting the number of months between the dates given that there are 2 consecutive flag=yes for the corresponding date interval.

v1 is the number of months between flag1 and flag2. So, if both flag1 and flag2 are yes, then I am wanting the value for v1.  If flag3 is also yes, then I want the total of v1 and v2.  However if flag3 is no, then I do not want that added to the total time.

Thanks

Super User
Posts: 23,683

## Re: help with sas code

This is untested - you should test all your edge cases.

I've only minimally verified it, but I think it's pretty close. There's some issues with variable types that you should make sure aren't in your code.

``````data want;
set have;

*Array declarations;
array flag(*) \$ flag1-flag22;
array dur(*) dur1-dur22;
array lab(*) lab1-lab22;
array val(*) v1-v21;

*Counter variables;;
index_dur=1;
start=0;
finish=0;

*Loop through flag variables;
do i=1 to dim(flag);

*Set start of series of yes;
if flag(i)='yes' and start=0 then
start=i;

*Set end of series;
if flag(i) in ('no', '') and start>0 then
do;
finish=i-1;

*Calculate start of summation;
do j=start to finish-1;
dur (index_dur)=sum(dur(index_dur), val(j));
end;

*reset all counters;
index_dur + 1;
start=0;
finish=0;
end;

*If start of missing series exit loop;
if flag(i)='' then
leave;
end;
run;``````
Occasional Contributor
Posts: 5

## Re: help with sas code

Hi,

Here is the data set as a datastep:

I believe this is what I was asked to provide.

data OUT.SASHELP;
infile datalines dsd truncover;
input Obs:BEST. Person:BEST. lab1:MMDDYY10. lab2:\$5. lab3:\$5. lab4:\$5. lab5:\$5. lab6:\$5. lab7:\$5. lab8:\$5. lab9:\$5. lab10:\$5. lab11:\$5. lab12:\$5. lab13:\$5. lab14:\$5. lab15:\$5. lab16:\$5. lab17:\$5. lab18:\$5. lab19:\$5. lab20:\$5. lab21:\$5. lab22:\$5. v1:\$2. v2:\$2. v3:\$2. v4:\$2. v5:\$2. v6:\$2. v7:\$2. v8:\$2. v9:\$1. v10:\$1. v11:\$1. v12:\$1. v13:\$1. v14:\$1. v15:\$1. v16:\$1. v17:\$1. v18:\$1. v19:\$1. v20:\$1. v21:\$1. flag1:\$3. flag2:\$3. flag3:\$3. flag4:\$3. flag5:\$3. flag6:\$3. flag7:\$3. flag8:\$3. flag9:\$3. flag10:\$3. flag11:\$3. flag12:\$3. flag13:\$3. flag14:\$3. flag15:\$2. flag16:\$2. flag17:\$2. flag18:\$2. flag19:\$2. flag20:\$3. flag21:\$3. flag22:\$3.;
datalines4;
1,1,11/07/2012,41304,41388,41570,41745,41836,41933,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,3,6,6,3,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,
2,2,01/28/2012,41106,41297,41443,41649,41656,41802,41919,41984,.,.,.,.,.,.,.,.,.,.,.,.,.,6,6,5,7,0,5,4,2,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,no,no,yes,yes,no,,,,,,,,,,,,,
3,3,01/24/2012,41054,41180,41370,41557,41766,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,4,7,6,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,
4,4,04/05/2012,41068,41226,41494,41803,41803,41939,41984,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,5,9,10,0,4,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
5,5,03/26/2012,41152,41395,41537,41666,41835,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,9,4,4,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,,,,,,,,,,,,,,,,
6,6,02/02/2012,41027,41984,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,32,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
7,7,01/11/2012,41058,41149,41347,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,3,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,no,no,,,,,,,,,,,,,,,,,,
8,8,01/11/2012,41122,41396,41570,41862,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,9,5,10,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,yes,yes,,,,,,,,,,,,,,,,,
9,9,04/14/2014,41793,41813,41841,41927,41982,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,0,1,3,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,yes,yes,,,,,,,,,,,,,,,,
10,10,04/11/2012,41114,41207,41472,41624,41733,41896,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,3,9,5,4,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,no,,,,,,,,,,,,,,,
11,11,04/27/2013,41780,41967,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,13,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,,,,,,,,,,,,,,,,,,,
12,12,04/20/2012,41180,41180,41376,41521,41641,41845,41990,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,0,7,5,4,6,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
13,13,09/16/2013,41969,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,14,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,,,,,,,,,,,,,,,,,,,,
14,14,12/30/2013,41836,41876,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,,,,,,,,,,,,,,,,,,,
15,15,06/19/2013,41514,41556,41605,41696,41794,41899,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,2,1,3,4,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,,,,,,,,,,,,,,,
16,16,01/03/2013,41443,41655,41802,41802,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,7,5,0,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,no,no,,,,,,,,,,,,,,,,,
17,17,06/27/2012,41262,41346,41523,41565,41710,41864,41946,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,3,6,1,5,5,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
18,18,02/09/2012,41113,41218,41358,41452,41554,41722,41911,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,4,4,3,4,5,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,no,,,,,,,,,,,,,,
19,19,05/07/2012,41148,41239,41330,41414,41512,41603,41694,41820,41890,41995,.,.,.,.,.,.,.,.,.,.,.,3,3,3,3,3,3,3,4,3,3,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,
20,20,06/08/2012,41169,41243,41400,41494,41624,41645,41764,41901,.,.,.,.,.,.,.,.,.,.,.,.,.,3,2,6,3,4,1,4,4,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,
21,21,03/20/2013,41460,41593,41764,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,4,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,yes,,,,,,,,,,,,,,,,,,
22,22,09/24/2012,41359,41547,41715,41974,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,6,6,9,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,,
23,23,01/07/2014,41830,41918,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
24,24,02/14/2012,40996,41213,41317,41629,41898,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,7,4,10,9,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,yes,no,no,,,,,,,,,,,,,,,,
25,25,07/19/2012,41445,41583,41680,41774,41857,41929,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,11,5,3,3,3,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,,,,,,,,,,,,,,,
26,26,01/31/2012,41016,41037,41072,41100,41121,41163,41191,41261,41296,41338,41366,41408,41436,41485,41485,41535,41618,41660,41828,41961,41975,3,1,1,1,0,2,1,2,1,2,1,1,1,1,0,2,3,1,6,4,1,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,yes,yes,yes
27,27,12/07/2012,41306,41418,41418,41626,41719,41852,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,3,0,7,3,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,yes,yes,yes,,,,,,,,,,,,,,,
28,28,09/10/2012,41302,41408,41521,41610,41752,41820,41929,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,4,4,3,4,2,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
29,29,05/07/2012,41261,41682,41782,41820,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,14,3,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,no,no,,,,,,,,,,,,,,,,,
30,30,09/02/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
31,31,06/19/2012,41218,41583,41704,41863,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,12,4,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,,
32,32,12/26/2013,41702,42003,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,9,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,no,,,,,,,,,,,,,,,,,,,
33,33,06/08/2012,41332,41578,41781,41879,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,8,8,7,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,,,,,,,,,,,,,,,,,
34,34,03/26/2013,41485,41485,41752,41799,41820,41874,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,0,9,2,0,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,,,,,,,,,,,,,,,
35,35,02/24/2012,41030,41179,41257,41418,41739,41892,41985,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,4,3,5,11,5,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
36,36,12/04/2012,41247,41247,41283,41283,41306,41324,41404,41487,41529,41593,41730,41864,41996,.,.,.,.,.,.,.,.,0,0,1,0,1,0,3,3,1,2,5,4,4,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,yes,no,no,yes,yes,yes,yes,,,,,,,,
37,37,04/02/2012,41249,41319,41540,41704,41792,41939,42002,.,.,.,.,.,.,.,.,.,.,.,.,.,.,8,2,7,6,3,4,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,no,,,,,,,,,,,,,,
38,38,04/28/2012,41029,41106,41506,41664,41888,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,0,3,13,5,8,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,yes,yes,yes,yes,,,,,,,,,,,,,,,,
39,39,01/24/2013,41330,41807,41807,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,16,0,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,,,,,,,,,,,,,,,,,,
40,40,07/19/2012,41129,41149,41229,41284,41354,41445,41577,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,0,3,2,2,3,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,no,,,,,,,,,,,,,,
41,41,07/22/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,,,,,,,,,,,,,,,,,,,,,
42,42,06/13/2012,41104,41207,41311,41424,41542,41634,41725,41848,.,.,.,.,.,.,.,.,.,.,.,.,.,1,3,4,3,4,3,3,4,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,
43,43,01/30/2013,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
44,44,02/28/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
45,45,05/22/2013,41620,41753,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
46,46,03/27/2012,41191,41422,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,,,,,,,,,,,,,,,,,,,
47,47,05/22/2012,41128,41296,41451,41577,41768,41886,41974,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,5,5,4,7,4,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
48,48,08/16/2012,41285,41365,41488,41662,41830,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,3,4,5,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,no,yes,yes,yes,,,,,,,,,,,,,,,,
49,49,11/14/2012,41393,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,,,,,,,,,,,,,,,,,,,,
50,50,10/31/2012,41411,41621,41750,41943,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,7,4,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,yes,no,yes,,,,,,,,,,,,,,,,,
51,51,03/26/2012,41493,41529,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,17,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
52,52,01/10/2013,41431,41758,41913,41946,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,10,6,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,no,no,no,,,,,,,,,,,,,,,,,
53,53,01/25/2012,41129,41283,41374,41486,41661,41815,41983,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,5,3,3,6,5,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
54,54,03/28/2012,41207,41309,41386,41477,41477,41547,41631,41957,.,.,.,.,.,.,.,.,.,.,.,.,.,7,4,2,3,0,2,3,11,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,
55,55,03/25/2013,41452,41599,41655,41839,41921,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,5,2,6,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,no,yes,yes,yes,,,,,,,,,,,,,,,,
56,56,05/14/2012,41218,41390,41662,41851,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,5,9,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,yes,no,,,,,,,,,,,,,,,,,
57,57,09/14/2012,41397,41612,41718,41941,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,8,7,3,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,yes,yes,yes,,,,,,,,,,,,,,,,,
58,58,03/05/2012,41059,41124,41209,41303,41384,41470,41564,41649,41730,41820,41850,41907,.,.,.,.,.,.,.,.,.,2,3,2,3,3,3,3,3,3,2,1,2,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,yes,yes,,,,,,,,,
59,59,03/09/2012,41081,41165,41256,41422,41670,41823,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,3,3,5,8,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,no,no,yes,no,no,,,,,,,,,,,,,,,
60,60,12/08/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,,,,,,,,,,,,,,,,,,,,,
61,61,06/12/2012,41101,41227,41283,41862,41878,41906,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,4,2,19,0,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,yes,yes,no,no,no,,,,,,,,,,,,,,,
62,62,07/31/2013,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
63,63,02/19/2013,41525,41631,41747,41842,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,3,4,3,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,yes,,,,,,,,,,,,,,,,,
64,64,01/04/2012,41030,41120,41240,41361,41450,41548,41663,41772,41935,41991,.,.,.,.,.,.,.,.,.,.,.,4,2,4,4,3,4,3,4,5,2,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,
65,65,02/13/2012,41064,41197,41289,41400,41547,41701,41808,41849,41953,.,.,.,.,.,.,.,.,.,.,.,.,4,4,3,4,4,6,3,1,4,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,yes,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,
66,66,05/06/2013,41598,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,,,,,,,,,,,,,,,,,,,,
67,67,04/30/2012,41101,41403,41533,41708,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,10,4,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,no,yes,yes,,,,,,,,,,,,,,,,,
68,68,06/04/2012,41173,41292,41429,41472,41514,41625,41759,41803,41901,41965,.,.,.,.,.,.,.,.,.,.,.,3,4,5,1,1,4,4,2,3,2,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,no,yes,yes,yes,no,no,no,no,,,,,,,,,,,
69,69,05/01/2012,41116,41219,41655,41828,41955,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,4,14,6,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,yes,yes,yes,yes,,,,,,,,,,,,,,,,
70,70,10/16/2013,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
71,71,06/13/2013,41445,41527,41536,41591,41681,41737,41754,41947,41989,.,.,.,.,.,.,.,.,.,.,.,.,0,3,0,2,3,2,0,7,1,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,no,no,no,,,,,,,,,,,,
72,72,01/26/2012,41054,41136,41438,41835,41863,41912,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,3,10,13,1,1,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,no,yes,yes,,,,,,,,,,,,,,,
73,73,01/16/2013,41290,41597,41649,41835,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,0,10,2,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,,,,,,,,,,,,,,,,,
74,74,10/09/2012,41352,41760,41940,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,14,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,no,,,,,,,,,,,,,,,,,,
75,75,03/06/2012,41165,41352,41781,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,6,14,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,yes,no,,,,,,,,,,,,,,,,,,
76,76,09/12/2012,41220,41220,41477,41477,41554,41965,41967,.,.,.,.,.,.,.,.,.,.,.,.,.,.,2,0,8,0,3,13,0,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,no,no,no,,,,,,,,,,,,,,
77,77,04/02/2012,41123,41184,41325,41487,41760,41821,41995,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,2,4,6,9,2,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,no,yes,yes,,,,,,,,,,,,,,
78,78,04/11/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,,,,,,,,,,,,,,,,,,,,,
79,79,01/07/2013,41414,41572,41677,41873,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,5,4,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,,
80,80,03/26/2014,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,,,,,,,,,,,,,,,,,,,,,
81,81,05/24/2012,41081,41240,41362,41509,41590,41764,41939,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,5,4,5,3,6,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,yes,yes,yes,yes,yes,,,,,,,,,,,,,,
82,82,08/07/2012,41128,41381,41612,41634,41720,41913,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,0,8,8,0,3,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,yes,yes,no,no,,,,,,,,,,,,,,,
83,83,08/23/2012,41284,41493,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,7,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,no,no,,,,,,,,,,,,,,,,,,,
84,84,09/25/2012,41528,41576,41772,41956,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,12,1,7,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,,
85,85,01/02/2012,40910,40910,40910,40966,41086,41191,41689,41778,.,.,.,.,.,.,.,.,.,.,.,.,.,0,0,0,1,4,4,16,3,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,no,yes,yes,yes,yes,,,,,,,,,,,,,
86,86,01/16/2013,41496,41682,41848,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,7,6,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,,,,,,,,,,,,,,,,,,
87,87,09/06/2013,41555,41716,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
88,88,01/18/2012,41017,41221,41431,41603,41795,41914,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,7,7,5,7,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,no,no,no,,,,,,,,,,,,,,,
89,89,08/25/2014,41904,41946,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,2,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,yes,,,,,,,,,,,,,,,,,,,
90,90,01/20/2012,41064,41194,41327,41449,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,5,4,4,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,yes,,,,,,,,,,,,,,,,,
91,91,04/23/2013,41473,41865,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,13,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,,,,,,,,,,,,,,,,,,,
92,92,02/28/2012,41361,41800,41947,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,13,15,5,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,yes,,,,,,,,,,,,,,,,,,
93,93,01/16/2013,41921,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,21,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,,,,,,,,,,,,,,,,,,,,
94,94,01/24/2012,40995,41072,41170,41281,41526,41541,41719,41849,.,.,.,.,.,.,.,.,.,.,.,.,.,2,3,3,4,8,0,6,4,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,yes,no,no,no,yes,no,yes,,,,,,,,,,,,,
95,95,01/10/2013,41366,41564,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,3,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,yes,yes,yes,,,,,,,,,,,,,,,,,,,
96,96,03/12/2013,41534,41597,41790,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,2,6,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,,,,,,,,,,,,,,,,,,
97,97,02/08/2012,41138,41304,41360,41542,41673,41757,41863,.,.,.,.,.,.,.,.,.,.,.,.,.,.,6,5,2,6,5,2,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,no,yes,yes,yes,yes,,,,,,,,,,,,,,
98,98,07/19/2012,41218,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,4,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,,,,,,,,,,,,,,,,,,,,
99,99,10/03/2012,41234,41297,41378,41646,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,1,2,3,9,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,no,no,yes,no,,,,,,,,,,,,,,,,,
100,100,05/11/2012,41054,41185,41424,41584,41855,41922,41922,.,.,.,.,.,.,.,.,.,.,.,.,.,.,0,5,7,6,9,2,0,.,.,.,.,.,.,.,.,.,.,.,.,.,.,no,yes,yes,no,no,yes,no,no,,,,,,,,,,,,,,
;;;;

PROC Star
Posts: 8,163

## Re: help with sas code

Not sure if I totally understand the logic, but try the following, check the numbers output, and see if it does what you are trying to accomplish:

```data want (drop=i counter pcounter sum);
set have;
array aflag(*) flag1-flag11;
array av(*) v1-v10;
array pr(4) prd_1-prd_4;
do i=1 to 11;
if i=1 then do;
counter=0;
pcounter=0;
sum=0;
end;
if aflag(i)='yes' then do;
if i eq 1 or aflag(i-1) eq 'no' then do;
sum=0;
counter=1;
end;
else counter+1;
if counter gt 1 then sum+av(i-1);
end;
if (aflag(i)='no' or missing(aflag(i)) or i eq 11) and counter gt 1 then do;
pcounter+1;
pr(pcounter)=sum;
sum=0;
counter=0;
end;
end;
run;
```

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 5

## Re: help with sas code

Hi,

I had to wait until today where I could have access to SAS again in order to test the code and I have not found an instance where it does not work yet.

Thank you all for the help.

I have not worked with arrays yet, and I'm still trying to come to  grips with them, so to have the help in seeing arrays put to action like this has helped me learn a lot.

Fantastic stuff and a big thank you!

Discussion stats
• 9 replies
• 222 views
• 0 likes
• 3 in conversation