<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Counting number of weeks of matches with varying starting dates for counting in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232909#M54777</link>
    <description>&lt;P&gt;1) Use INTCK function to calculate starting week number based on startdate.&lt;/P&gt;
&lt;P&gt;2) Add all week variables to array (no need to rename but have to add year by year eg: c0101-c0152 c0201-c0252 ...)&lt;/P&gt;
&lt;P&gt;3) Start do loop from starting week number to 626&lt;/P&gt;
&lt;P&gt;4) calculate count or sum as needed.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Nov 2015 16:07:05 GMT</pubDate>
    <dc:creator>ndp</dc:creator>
    <dc:date>2015-11-03T16:07:05Z</dc:date>
    <item>
      <title>Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232873#M54762</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this is about counting&amp;nbsp;horizontally from different points for each ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each row represent an individual over&amp;nbsp;12&amp;nbsp;years in week increments (52 sometimes 53 weeks in the year).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;For every time (week) there is a match with the code 1, 2 or 3 it is to count for that ID, but it must be depending on the startdate if there is one - if there is no startdate it just has to be counting from the first time it meets a 1&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;, 2 or&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt; 3. I want to know how many had 78 counts (for 78 weeks - but not for 78 consecutive weeks, gaps are allowed: that is missings in between or other classifications than 1, 2, 3) from when it starts counting.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So&amp;nbsp;variables go from c0101 to&amp;nbsp;&amp;nbsp;c1252 (that's a lot - shown below in short&amp;nbsp;form) ... that means from the year 2001 and the 1st week to the year 2012 and the 52nd week. c0833 would be the 33rd week of year 2008.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a million rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    c0101     c0102     c0103     c0104     ...    c0833 ...    c1252    Startdate
1         1      1           1        1                                      01JAN01   
2                                     2                 2                     
3         5                  5        5                 1            1       29MAY08   
4         1      1                                                           15MAR04
5         
6         
7                           3                            1                       
8
9         99               99         99                                     25APR03
10                          8          8
11
12                                                                          17FEB06
13
14        1               1             1
15
16&lt;/PRE&gt;
&lt;P&gt;I hope someone could give me an idea of what to use. Was thinking of temporary arrays, but there will need to be many of them...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 14:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232873#M54762</guid>
      <dc:creator>C_andrade</dc:creator>
      <dc:date>2015-11-03T14:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232883#M54766</link>
      <description>I still feel question is not so clear ..</description>
      <pubDate>Tue, 03 Nov 2015 14:41:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232883#M54766</guid>
      <dc:creator>pearsoninst</dc:creator>
      <dc:date>2015-11-03T14:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232890#M54770</link>
      <description>&lt;P&gt;Hi Pearson,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry. I will try to recapitulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Dataset consists of&amp;nbsp;several horizontal rows, each one representing an individual.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First column identifies the individual.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then columns represent one week after another. Btw&amp;nbsp;all the week-variables are texts - pondering about making these numerics.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are week variables from the first week of 2001 to the last week of 2012 - giving some 626 variables&amp;nbsp;of these (10 * 52 weeks + 2 * 53 weeks - 2004 and 2009 have 53 weeks). To me they are named&amp;nbsp;oddly so that the first 52 variables&amp;nbsp;run from c0101 to c0152 and the next variable is c0201 and so on ... up to c1252 - pondering about renaming the variables in a way that makes more sense if I were to use array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For&amp;nbsp;every week to every individual there&amp;nbsp;can be a status code else it is&amp;nbsp;empty. The status codes I am out for are 1, 2 or&amp;nbsp;3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to know how many individuals reached 78 of the status codes 1,2 or&amp;nbsp;3 over the time period. But the twist is that there may also be defined a startdate (last column) for when it makes sense to count from. If there is no startdate then it should simply just starting counting from when it meets the first&amp;nbsp;1,2 or 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope it makes sense!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 15:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232890#M54770</guid>
      <dc:creator>C_andrade</dc:creator>
      <dc:date>2015-11-03T15:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232895#M54773</link>
      <description>&lt;P&gt;Well, the simplest answer is to normalise your data, go from wide data, to long data (there are many posts and papers on this topic). &amp;nbsp;Then your data might look something like:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; STARTDATE &amp;nbsp; &amp;nbsp; WEEKNO &amp;nbsp; &amp;nbsp; &amp;nbsp;CODE&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then its quite simple, you could create a flag on the row where STARTDATE week is matching:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; STARTDATE &amp;nbsp; &amp;nbsp; WEEKNO &amp;nbsp; &amp;nbsp; &amp;nbsp;CODE &amp;nbsp; &amp;nbsp;STARTFLAG&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then its simple aggregates to sum not null records where weekno &amp;gt; min(weekno) where starflag="Y". &amp;nbsp;I can try to get some code out later on, or maybe tomorrow as in meeting all day.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 15:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232895#M54773</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-11-03T15:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232900#M54775</link>
      <description>From the start date determine what your starting index should be, ie 01Jan01 maps to C0101 and that maps to 1. &lt;BR /&gt;&lt;BR /&gt;So loop from 1 to dimension of array or number of columns and count the number of 1/2/3 allowing your loop to break if you hit 78 since you've met your metric. &lt;BR /&gt;&lt;BR /&gt;Figuring out the starting index is the hardest part, relatively. It will be something like (year-year(start))*52 +week(year).&lt;BR /&gt;&lt;BR /&gt;And then something like the following (untested)&lt;BR /&gt;&lt;BR /&gt;count=0;&lt;BR /&gt;do i=index to num_columns while(count&amp;lt;78);&lt;BR /&gt;if col(i) in  (1, 2,3) then count+1;&lt;BR /&gt;end;&lt;BR /&gt;</description>
      <pubDate>Tue, 03 Nov 2015 15:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232900#M54775</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-03T15:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232909#M54777</link>
      <description>&lt;P&gt;1) Use INTCK function to calculate starting week number based on startdate.&lt;/P&gt;
&lt;P&gt;2) Add all week variables to array (no need to rename but have to add year by year eg: c0101-c0152 c0201-c0252 ...)&lt;/P&gt;
&lt;P&gt;3) Start do loop from starting week number to 626&lt;/P&gt;
&lt;P&gt;4) calculate count or sum as needed.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 16:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/232909#M54777</guid>
      <dc:creator>ndp</dc:creator>
      <dc:date>2015-11-03T16:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233395#M54825</link>
      <description>&lt;P&gt;Here is what I have coded so far. But I haven't worked how to incorporate the start date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
set have;
array weeks{*} c0101--c1252;
sum=0;
d _i_ = 1 to 626 until (first_1 &amp;gt; .);
if weeks{_i_} in ('1','2','3') then first_1=_i_;
end;
if (. lt first_1 le 626) then do _i_ = first_1 to min(first_1 + 77, dim(weeks));
sum + weeks{_i_}; 
end;
count=0;
do i=1 to 721 while(count&amp;lt;78);
if weeks(i) in ('1','2','3') then count+1;
end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Nov 2015 09:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233395#M54825</guid>
      <dc:creator>C_andrade</dc:creator>
      <dc:date>2015-11-06T09:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233433#M54828</link>
      <description>&lt;P&gt;To incorporate start date you need to create a variable that stores week number of the start date. Following will work as long as your start date is not 31DEC2012.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if nmiss(startdate)=0 then startweek=ceil(('01JAN2001'd-startdate+1)/7));&lt;/P&gt;
&lt;P&gt;else startweek=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot set up array in this fashion since the weeks are not sequentially numbered for eg there is no column c0153. You have to add range for each year separately. Use following statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array weeks{*} c0101-c0152 c0201-c0252 ...;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then your do loop should start from calculated startweek&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do _i_=startweek to dim(temp);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 14:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233433#M54828</guid>
      <dc:creator>ndp</dc:creator>
      <dc:date>2015-11-06T14:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of weeks of matches with varying starting dates for counting</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233691#M54842</link>
      <description>&lt;P&gt;In order to pick the correct variable which matches with a start date you need to know exactly how these variables have been created and use the same algorithm. There are some challenges in that like below example illustrates.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  picture yyww (default=4)
    other='%0y%0V' (datatype=date)
  ;
quit;

data test;
  format date date9.;
  date='01jan2001'd;
  YearWeek=put(date,yyww.);
  week=week(date);
  output;
  date='01jan2012'd;
  YearWeek=put(date,yyww.);
  week=week(date);
  output;
  date='30dec2012'd;
  YearWeek=put(date,yyww.);
  week=week(date);
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/786i9E7D556AFAC1095F/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below example code where I've made up the year and week calculation. The hardedst bit was to create some sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 1&lt;/STRONG&gt;:&amp;nbsp;Create sample data&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id 8 yw $4 var 8;
  format StartDate date9.;
  format _date date9.;
  do id=1 to 100;
    StartDate=floor(ranuni(1)*2000) +'01jan2005'd;
    _date=StartDate;
    do while(_date&amp;lt;='31Dec2015'd);
      yw=put(_date,year2.)||put(ceil((_date-intnx('year',_date,-1,'e'))/7),z2.);
      var=ceil(ranuni(1)*14);
      if var&amp;gt;5 then call missing(var);
      output;
      _date=_date+7;
    end; 
  end;
run;

proc transpose data=have out=source(drop=_:) prefix=c;
  by id;
  id yw;
  var var;
run;
proc sql noprint;
  select name into :varlist separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='SOURCE' and upcase(name) like 'C%'
  order by name
  ;
quit;

data source;
  length id StartDate &amp;amp;varlist 8;
  format StartDate date9.;
  merge have(keep=id StartDate) source;
  by id;
  if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 2&lt;/STRONG&gt;: Count 1,2,3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The important parts in the code:&lt;/P&gt;
&lt;P&gt;- Variables in array must be in sorted order (from earliest to latest "date").&lt;/P&gt;
&lt;P&gt;- The hash holds the number of the array element - key is the variable name&lt;/P&gt;
&lt;P&gt;- The algorithm used to calculate the starting variable name based on start date must use the same logic than what has been used to create the variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
  length Count123 8;
  set source;
  array vars {*} &amp;amp;varlist;
  if _n_=1 then 
    do;
      length _varname $32 _arr_element 8;
      dcl hash h();
      _rc=h.defineKey('_varname');
      _rc=h.defineData('_arr_element');
      _rc=h.defineDone();
      do _arr_element=1 to dim(vars);
        _varname=upcase(vname(vars[_arr_element]));
        _rc=h.add();
      end;
    end;

  if missing(StartDate) then _arr_element=1;
  else
    do;
      _varname='C'||put(StartDate,year2.)||put(ceil((StartDate-intnx('year',StartDate,-1,'e'))/7),z2.);
      _rc=h.find();
    end;

  do _i=_arr_element to dim(vars);
    if vars[_i] in (1,2,3) then Count123=sum(Count123,1);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2015 20:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-number-of-weeks-of-matches-with-varying-starting-dates/m-p/233691#M54842</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-11-08T20:40:59Z</dc:date>
    </item>
  </channel>
</rss>

