<?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: How to create new variable count, that counts the total observation  number within same idividua in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942917#M369671</link>
    <description>Thank you soooo much! I modified a little bit based on your code and it works</description>
    <pubDate>Fri, 06 Sep 2024 15:44:59 GMT</pubDate>
    <dc:creator>Hd1000204</dc:creator>
    <dc:date>2024-09-06T15:44:59Z</dc:date>
    <item>
      <title>How to create new variable count, that counts the total observation  number within same idividuals?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942818#M369653</link>
      <description>&lt;P&gt;Hi there!&lt;/P&gt;&lt;P&gt;I'm trying to create a new variable "count" that counts how many visits were within each period.&lt;/P&gt;&lt;P&gt;For example: My original data looks like this:&lt;/P&gt;&lt;P&gt;id&amp;nbsp; hosp_adm_date&amp;nbsp; period_end&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 11/10/2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 11/10/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;1 01/12/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01/12/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; 02/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/09/2019&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;1 02/17/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/17/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;1 02/28/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/28/2019&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;1 03/15/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/15/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;1 04/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04/09/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;2&amp;nbsp;04/09/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/09/2015&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 04/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;04/09/2018&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create table something like this:&lt;/P&gt;&lt;P&gt;id&amp;nbsp; hosp_adm_date&amp;nbsp; period_end&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;counts&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 11/10/2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 11/10/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;1 01/12/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01/12/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;BR /&gt;1&amp;nbsp; 02/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/09/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;1 02/17/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/17/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;1 02/28/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/28/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;1 03/15/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/15/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1 04/09/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04/09/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2&amp;nbsp;04/09/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/09/2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2 04/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;04/09/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ex. For id 1's first period&amp;nbsp;11/10/2017- 11/10/2018 , there will be 6 visits within them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do that? I have tried several codes but not working. I would be really appreciate if someone can help with this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 22:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942818#M369653</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-05T22:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942824#M369655</link>
      <description>&lt;P&gt;From your problem description I was expecting you to share TWO input datasets.&amp;nbsp; One with the PERIODS and the other with the ADMISSION dates.&amp;nbsp; Why do you only show ONE dataset?&amp;nbsp; How can there be more than one ADMISSION per hospital stay?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 00:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942824#M369655</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-06T00:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942827#M369656</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  (hosp_adm_date  period_end ) (: mmddyy12.);
format  hosp_adm_date  period_end mmddyy10.;
cards;
1  11/10/2017            11/10/2018       
1 01/12/2018            01/12/2019      
1  02/09/2018          02/09/2019    
1 02/17/2018            02/17/2019     
1 02/28/2018           02/28/2019    
1 03/15/2018           03/15/2019      
1 04/09/2018          04/09/2019     
2 04/09/2015         04/09/2016 
2 04/09/2018        04/09/2018
;

proc sql;
create table want as
select *,(select count(*) from have where id=a.id and hosp_adm_date between a.hosp_adm_date and a.period_end) as count
 from have as a
  order by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Sep 2024 01:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942827#M369656</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-06T01:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942833#M369657</link>
      <description>&lt;P&gt;Thank you for your question. I apologize for not explaining the dataset clearly&lt;/P&gt;&lt;P&gt;This dataset captures all hospital admission events for different subjects in a long-form format. Each row represents a unique admission event. The "period end" is calculated as 12 months after each unique hospital admission date for a specific ID.&lt;/P&gt;&lt;P&gt;We're working with a single dataset that contains multiple admissions per subject. What I'm trying to determine is how many events occurred within each 12-month period following a unique hospital admission date for each subject.&lt;/P&gt;&lt;P&gt;There aren't two separate datasets for periods and admission dates. Instead, we're using the admission dates to define the start of each period, and then counting events within those periods.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 02:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942833#M369657</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-06T02:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942834#M369658</link>
      <description>Thank you for your help, I've tried the code but the count result isn't exactly what I'm looking for. I've provided more details about my exact requirements in replies to previous threads. Thanks again for your assistance.</description>
      <pubDate>Fri, 06 Sep 2024 02:26:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942834#M369658</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-06T02:26:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942837#M369659</link>
      <description>&lt;P&gt;So you want to count re-admissions that occurred within one year of the original admission?&lt;/P&gt;
&lt;P&gt;Did you try something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id admdate :mmddyy. ;
  format admdate yymmdd10.;
cards;
1 11/10/2017 
1 01/12/2018
1 02/09/2018
1 02/17/2018
1 02/28/2018
1 03/15/2018
1 04/09/2018
2 04/09/2016
2 04/09/2018
;

proc sql;
create table want as
  select a.*,count(b.id) as n_readmission
  from have a
  left join have b
   on a.id=b.id
   and b.admdate between (a.admdate+1) and intnx('year',a.admdate,1,'s')
  group by a.id,a.admdate
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    id       admdate    n_readmission

 1      1    2017-11-10          6
 2      1    2018-01-12          5
 3      1    2018-02-09          4
 4      1    2018-02-17          3
 5      1    2018-02-28          2
 6      1    2018-03-15          1
 7      1    2018-04-09          0
 8      2    2016-04-09          0
 9      2    2018-04-09          0
&lt;/PRE&gt;
&lt;P&gt;Notice how the last observations have zero re-admissions. If you want those to have 1 instead then you could remove the +1 in the ON condition. But then all of the counts will go up by one since you will be counting the current admission record every time.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 03:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942837#M369659</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-06T03:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942844#M369661</link>
      <description>So did you try Tom's code ?</description>
      <pubDate>Fri, 06 Sep 2024 03:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942844#M369661</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-06T03:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942874#M369665</link>
      <description>&lt;P&gt;Use a date-indexed array and a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id admdate :mmddyy. ;
format admdate yymmdd10.;
cards;
1 11/10/2017 
1 01/12/2018
1 02/09/2018
1 02/17/2018
1 02/28/2018
1 03/15/2018
1 04/09/2019
2 04/09/2016
2 04/09/2018
;

%let start = %sysfunc(inputn(1900-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2099-12-31,yymmdd10.));

data want;
array ev {&amp;amp;start.:&amp;amp;end.} _temporary_;
do i = &amp;amp;start. to &amp;amp;end.;
  ev{i} = 0;
end;
do until (last.id);
  set have;
  by id;
  ev{admdate} = 1;
end;
do until (last.id);
  set have;
  by id;
  n_readmission = 0;
  do i = admdate to intnx("year",admdate,1,"s");
    n_readmission + ev{i};
  end;
  output;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Sep 2024 11:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942874#M369665</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-09-06T11:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942917#M369671</link>
      <description>Thank you soooo much! I modified a little bit based on your code and it works</description>
      <pubDate>Fri, 06 Sep 2024 15:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942917#M369671</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-06T15:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942918#M369672</link>
      <description>Yes! It works. Thank you for helping</description>
      <pubDate>Fri, 06 Sep 2024 15:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942918#M369672</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-06T15:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new variable count, that counts the total observation  number within same idividua</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942919#M369673</link>
      <description>These codes also works! Thank you for helping</description>
      <pubDate>Fri, 06 Sep 2024 15:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-variable-count-that-counts-the-total/m-p/942919#M369673</guid>
      <dc:creator>Hd1000204</dc:creator>
      <dc:date>2024-09-06T15:49:20Z</dc:date>
    </item>
  </channel>
</rss>

