<?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 do calculation against first value for each id? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251386#M47492</link>
    <description>&lt;P&gt;I also added a sort, just in case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Date :date.;
format date date9.;
datalines;
001   01Jan2012 
001   01Feb2012 
001   15Mar2012 
001   01Apr2013     
001   06Mar2013 
002   01Jan2012 
002   01Feb2012 
;

proc sort data=have; by id date; run;

data want;
seq = 0;
do until(last.id);
    set have; by id;
    if first.id then plantDate = date;
    seq + 1;
    gap = intck("DAY", coalesce(prevDate, date), date);
    want = intck("DAY", plantDate, date);
    output;
    prevDate = date;
    end;
drop plantDate prevDate;
run;

proc print data=want noobs; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 21 Feb 2016 06:18:03 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-02-21T06:18:03Z</dc:date>
    <item>
      <title>How to do calculation against first value for each id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251383#M47489</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have a silly quesiton but really want your help.&lt;/P&gt;
&lt;P&gt;I have a plant growth data and it looks like this in the following has [ID, date]&lt;/P&gt;
&lt;P&gt;I would like to calcualte date interval &amp;nbsp;against each first_date (planting date for each plant)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there an efficient way to calcualte the "want"?&lt;/P&gt;
&lt;P&gt;I have tried to use a data step to set by ID and date and create an ID first_dt and merge this back to the original dataset&lt;/P&gt;
&lt;P&gt;and calcuate based on that. &amp;nbsp;But is there a better way or even a macro coding method?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any efficient logic is appreicated. &amp;nbsp;because I would need to a lot of this kind of claculaton for next steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks all for your help!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; want&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; 01Jan2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; 01Feb2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; 15Mar2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 63&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; 01Apr2013 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;79 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; 06Mar2013 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 105&lt;/P&gt;
&lt;P&gt;002 &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;01Jan2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;002 &amp;nbsp;&amp;nbsp;0&lt;SPAN&gt;1Feb2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/SPAN&gt;&amp;nbsp; &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;003&lt;/P&gt;
&lt;P&gt;003&lt;/P&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2016 05:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251383#M47489</guid>
      <dc:creator>Lulus</dc:creator>
      <dc:date>2016-02-21T05:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to do calculation against first value for each id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251384#M47490</link>
      <description>&lt;P&gt;Good candidate task for a DOW loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Date :date.;
format date date9.;
datalines;
001   01Jan2012 
001   01Feb2012 
001   15Mar2012 
001   01Apr2013     
001   06Mar2013 
002   01Jan2012 
002   01Feb2012 
;

data want;
do until(last.id);
    set have; by id;
    if first.id then plantDate = date;
    want = intck("DAY", plantDate, date);
    output;
    end;
drop plantDate;
run;

proc print data=want noobs; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Feb 2016 05:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251384#M47490</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-02-21T05:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to do calculation against first value for each id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251385#M47491</link>
      <description>&lt;P&gt;thanks so much! this is eactly I needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;can I ask two follow up quesitons.&lt;/P&gt;
&lt;P&gt;1. I would also like to add "gap" which would give me diffrence from current date to previous date (the first added column)&lt;/P&gt;
&lt;P&gt;2. sequence of measurement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;both value is based on unique ID. &amp;nbsp;thank you in advance for helping.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;001   01Jan2012    0    1
001   01Feb2012    28   2
001   15Mar2012    45   3
001   01Apr2013    16   4
001   06Mar2013    36   5
002   01Jan2012     0   1
002   01Feb2012    28   2&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2016 06:03:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251385#M47491</guid>
      <dc:creator>Lulus</dc:creator>
      <dc:date>2016-02-21T06:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to do calculation against first value for each id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251386#M47492</link>
      <description>&lt;P&gt;I also added a sort, just in case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Date :date.;
format date date9.;
datalines;
001   01Jan2012 
001   01Feb2012 
001   15Mar2012 
001   01Apr2013     
001   06Mar2013 
002   01Jan2012 
002   01Feb2012 
;

proc sort data=have; by id date; run;

data want;
seq = 0;
do until(last.id);
    set have; by id;
    if first.id then plantDate = date;
    seq + 1;
    gap = intck("DAY", coalesce(prevDate, date), date);
    want = intck("DAY", plantDate, date);
    output;
    prevDate = date;
    end;
drop plantDate prevDate;
run;

proc print data=want noobs; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Feb 2016 06:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-calculation-against-first-value-for-each-id/m-p/251386#M47492</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-02-21T06:18:03Z</dc:date>
    </item>
  </channel>
</rss>

