<?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: Calculation number of different observations in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41421#M10703</link>
    <description>Benjy has the right idea but hope I can add a little value&lt;BR /&gt;
 &amp;gt; use RETAIN. &lt;BR /&gt;
 &lt;BR /&gt;
    RETAIN N  0 ; /* N is  retained but starts with 0 */&lt;BR /&gt;
* so then don't need the line IF _N_=1 THEN N=0 ; &lt;BR /&gt;
    N +1 ; /* accumulate N each time through this statemen.*/</description>
    <pubDate>Sun, 03 Apr 2011 14:19:01 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2011-04-03T14:19:01Z</dc:date>
    <item>
      <title>Calculation number of different observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41418#M10700</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I've created an example in paint of how my dataset should look like (made a mistake with analys code 000001, the experience should of course go to 4 instead of 5):&lt;BR /&gt;
&lt;A href="http://img850.imageshack.us/img850/696/exampled.png" target="_blank"&gt;http://img850.imageshack.us/img850/696/exampled.png&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
At this moment I'm having the timeID and Analys variables and I should calculate the experience.&lt;BR /&gt;
&lt;BR /&gt;
I was thinking about something like this:&lt;BR /&gt;
&lt;BR /&gt;
data experience;&lt;BR /&gt;
set experience;&lt;BR /&gt;
if analys ne lag(analys) then do;&lt;BR /&gt;
                                  experience=1;&lt;BR /&gt;
                                end;&lt;BR /&gt;
   else experience=experience+1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The problem however is that it only puts a 1 on the first row when the analystcode changes but the other observations give experience=.&lt;BR /&gt;
&lt;BR /&gt;
Anyone knows what I'm doing wrong here?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks a lot!

Message was edited by: jebuske</description>
      <pubDate>Thu, 31 Mar 2011 12:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41418#M10700</guid>
      <dc:creator>jebuske</dc:creator>
      <dc:date>2011-03-31T12:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation number of different observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41419#M10701</link>
      <description>I just found a solution. I believe it should be working correctly.&lt;BR /&gt;
&lt;BR /&gt;
data experience;&lt;BR /&gt;
set experience;&lt;BR /&gt;
if analys ne lag(analys) then do;&lt;BR /&gt;
                                 experience=1;&lt;BR /&gt;
				 totaal=1;&lt;BR /&gt;
                                end;&lt;BR /&gt;
   else experience=totaal;&lt;BR /&gt;
   totaal+1;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 31 Mar 2011 12:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41419#M10701</guid>
      <dc:creator>jebuske</dc:creator>
      <dc:date>2011-03-31T12:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation number of different observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41420#M10702</link>
      <description>I suggest using the retain statement.&lt;BR /&gt;
DATA;&lt;BR /&gt;
RETAIN N ; /* N is set to missing and will be retained in each subsequent iteration */&lt;BR /&gt;
IF _N_=1 THEN N=0 ; /*first case N=0.  All other cases N is still missing */&lt;BR /&gt;
N=SUM(N, +1) ; /*FOR FIRST CASE N=0+1. The value of 1 is carried over to the second case and then 1 is added to it so the second case becomes 2. For the third case the value of 2 is retained, i.e. carries over, and 1 is added to this so N for the 3rd case is 3.  And so on.*/&lt;BR /&gt;
/* Using N=N+1 would work here as well as N=SUM(N, 1) but the latter form is good practice because it insures that a missing value for N will not make the result of the addition null or missing.*/</description>
      <pubDate>Thu, 31 Mar 2011 13:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41420#M10702</guid>
      <dc:creator>Benjy</dc:creator>
      <dc:date>2011-03-31T13:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation number of different observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41421#M10703</link>
      <description>Benjy has the right idea but hope I can add a little value&lt;BR /&gt;
 &amp;gt; use RETAIN. &lt;BR /&gt;
 &lt;BR /&gt;
    RETAIN N  0 ; /* N is  retained but starts with 0 */&lt;BR /&gt;
* so then don't need the line IF _N_=1 THEN N=0 ; &lt;BR /&gt;
    N +1 ; /* accumulate N each time through this statemen.*/</description>
      <pubDate>Sun, 03 Apr 2011 14:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41421#M10703</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-04-03T14:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation number of different observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41422#M10704</link>
      <description>Jebuske,&lt;BR /&gt;
&lt;BR /&gt;
I probably don't understand what you want but, if I do, I think you can get away with something like:&lt;BR /&gt;
&lt;BR /&gt;
data experience;&lt;BR /&gt;
  set experience;&lt;BR /&gt;
  by analys notsorted;&lt;BR /&gt;
  if first.analys then experience=1;&lt;BR /&gt;
  else experience+1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The by 'analysis notsorted' is just the way I like to code .. it has the same effect as your use of lag().  experience is automatically retained because of your use of the form experience+1.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Sun, 03 Apr 2011 19:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculation-number-of-different-observations/m-p/41422#M10704</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-04-03T19:01:50Z</dc:date>
    </item>
  </channel>
</rss>

