<?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 Creating a variable indicating day number per person in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788357#M252038</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a repeated-measures dataset in long format with each person represented by the numeric variable "ID." Each ID has a date for each row, with the number of rows/dates per person ranging from 3 to 16. The dates per person are mostly consecutive, but there is some data missingness, such that a date for a person may skip from "9/1/2014" to "9/3/2014" for the next row. I would like to create a variable that is "1" for the very first date for each person, and then the following rows represent the number of days from that first date (plus 1). Below is the sample code for what I would like, with the variable "want" representing the variable I want to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data;
input ID date : mmddyy10. want;
format date mmddyy10.;
datalines;
1000007	10/26/2014	1
1000007	10/27/2014	2
1000007	10/28/2014	3
1000007	10/30/2014	5
1000007	10/31/2014	6
1000007	11/1/2014	7
1000007	11/2/2014	8
1000011	9/1/2014	1
1000011	9/3/2014	3
1000011	9/4/2014	4
1000011	9/7/2014	7
1000011	9/8/2014	8
1000011	9/9/2014	9
1000055	9/23/2015	1
1000055	9/24/2015	2
1000055	9/25/2015	3
1000055	9/28/2015	6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, for ID &lt;CODE class=" language-sas"&gt;1000007&lt;/CODE&gt;, the first date (&lt;CODE class=" language-sas"&gt;10/26/2014&lt;/CODE&gt;) is given a 1, followed by 2 for&lt;CODE class=" language-sas"&gt;&amp;nbsp;10/27/2014&lt;/CODE&gt; and 3 for &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt;. Since &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt; skips to &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; for the next row, &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; is given a 5 (4 days from the original first date, +1). And so on. The next ID (&lt;CODE class=" language-sas"&gt;1000011&lt;/CODE&gt;) again begins with 1 for the "want" variable and follows this pattern.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your assistance.&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jan 2022 22:17:45 GMT</pubDate>
    <dc:creator>confooseddesi89</dc:creator>
    <dc:date>2022-01-04T22:17:45Z</dc:date>
    <item>
      <title>Creating a variable indicating day number per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788357#M252038</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a repeated-measures dataset in long format with each person represented by the numeric variable "ID." Each ID has a date for each row, with the number of rows/dates per person ranging from 3 to 16. The dates per person are mostly consecutive, but there is some data missingness, such that a date for a person may skip from "9/1/2014" to "9/3/2014" for the next row. I would like to create a variable that is "1" for the very first date for each person, and then the following rows represent the number of days from that first date (plus 1). Below is the sample code for what I would like, with the variable "want" representing the variable I want to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data;
input ID date : mmddyy10. want;
format date mmddyy10.;
datalines;
1000007	10/26/2014	1
1000007	10/27/2014	2
1000007	10/28/2014	3
1000007	10/30/2014	5
1000007	10/31/2014	6
1000007	11/1/2014	7
1000007	11/2/2014	8
1000011	9/1/2014	1
1000011	9/3/2014	3
1000011	9/4/2014	4
1000011	9/7/2014	7
1000011	9/8/2014	8
1000011	9/9/2014	9
1000055	9/23/2015	1
1000055	9/24/2015	2
1000055	9/25/2015	3
1000055	9/28/2015	6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, for ID &lt;CODE class=" language-sas"&gt;1000007&lt;/CODE&gt;, the first date (&lt;CODE class=" language-sas"&gt;10/26/2014&lt;/CODE&gt;) is given a 1, followed by 2 for&lt;CODE class=" language-sas"&gt;&amp;nbsp;10/27/2014&lt;/CODE&gt; and 3 for &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt;. Since &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt; skips to &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; for the next row, &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; is given a 5 (4 days from the original first date, +1). And so on. The next ID (&lt;CODE class=" language-sas"&gt;1000011&lt;/CODE&gt;) again begins with 1 for the "want" variable and follows this pattern.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your assistance.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 22:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788357#M252038</guid>
      <dc:creator>confooseddesi89</dc:creator>
      <dc:date>2022-01-04T22:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable indicating day number per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788358#M252039</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a new variable called START_DATE that you initialize at the beginning of each ID. Use RETAIN to hold that value across the rows until it resets at the next ID. Subtract the date from the start_date to get the difference. This assumes your data is sorted and ordered by ID and DATE. If not, sort it ahead of time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set data;

by ID date;
retain start_date;

if first.ID then start_date = date;

want = date-start_date;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275602"&gt;@confooseddesi89&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a repeated-measures dataset in long format with each person represented by the numeric variable "ID." Each ID has a date for each row, with the number of rows/dates per person ranging from 3 to 16. The dates per person are mostly consecutive, but there is some data missingness, such that a date for a person may skip from "9/1/2014" to "9/3/2014" for the next row. I would like to create a variable that is "1" for the very first date for each person, and then the following rows represent the number of days from that first date (plus 1). Below is the sample code for what I would like, with the variable "want" representing the variable I want to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data;
input ID date : mmddyy10. want;
format date mmddyy10.;
datalines;
1000007	10/26/2014	1
1000007	10/27/2014	2
1000007	10/28/2014	3
1000007	10/30/2014	5
1000007	10/31/2014	6
1000007	11/1/2014	7
1000007	11/2/2014	8
1000011	9/1/2014	1
1000011	9/3/2014	3
1000011	9/4/2014	4
1000011	9/7/2014	7
1000011	9/8/2014	8
1000011	9/9/2014	9
1000055	9/23/2015	1
1000055	9/24/2015	2
1000055	9/25/2015	3
1000055	9/28/2015	6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, for ID &lt;CODE class=" language-sas"&gt;1000007&lt;/CODE&gt;, the first date (&lt;CODE class=" language-sas"&gt;10/26/2014&lt;/CODE&gt;) is given a 1, followed by 2 for&lt;CODE class=" language-sas"&gt;&amp;nbsp;10/27/2014&lt;/CODE&gt; and 3 for &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt;. Since &lt;CODE class=" language-sas"&gt;10/28/2014&lt;/CODE&gt; skips to &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; for the next row, &lt;CODE class=" language-sas"&gt;10/30/2014&lt;/CODE&gt; is given a 5 (4 days from the original first date, +1). And so on. The next ID (&lt;CODE class=" language-sas"&gt;1000011&lt;/CODE&gt;) again begins with 1 for the "want" variable and follows this pattern.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your assistance.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 22:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788358#M252039</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-04T22:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable indicating day number per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788365#M252044</link>
      <description>&lt;P&gt;Not recommending, but fun-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data data;
input ID date : mmddyy10. want;
format date mmddyy10.;
datalines;
1000007	10/26/2014	1
1000007	10/27/2014	2
1000007	10/28/2014	3
1000007	10/30/2014	5
1000007	10/31/2014	6
1000007	11/1/2014	7
1000007	11/2/2014	8
1000011	9/1/2014	1
1000011	9/3/2014	3
1000011	9/4/2014	4
1000011	9/7/2014	7
1000011	9/8/2014	8
1000011	9/9/2014	9
1000055	9/23/2015	1
1000055	9/24/2015	2
1000055	9/25/2015	3
1000055	9/28/2015	6
;
run;

proc sql;
 create table want as
 select *, date-min(date)+1 as want2
 from data
 group by id
 order by id, date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 22:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788365#M252044</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-04T22:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable indicating day number per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788366#M252045</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks, almost perfect! Just needed to add a "1 + " to the calculation of the want variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;want = 1+(date-start_date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;confooseddesi90&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 22:51:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-indicating-day-number-per-person/m-p/788366#M252045</guid>
      <dc:creator>confooseddesi89</dc:creator>
      <dc:date>2022-01-04T22:51:42Z</dc:date>
    </item>
  </channel>
</rss>

