<?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 retrieve the previous value to do the calculation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443354#M110908</link>
    <description>&lt;P&gt;You will need to clarify your logic for me.&amp;nbsp; Why does the third row change and where does the 6 come from?&amp;nbsp; Note you can't use lag in this instance because you are changing the lag() value.&amp;nbsp; You would need to use a retained variable, e.g&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lsttimes;
  by id examlevel;
  if first.examlevel then lsttimes=times;
  else do;
    if...then times=lsttimes-2;
  end;
run;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Mar 2018 15:29:51 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-03-07T15:29:51Z</dc:date>
    <item>
      <title>how to retrieve the previous value to do the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443350#M110907</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some data in which each examee may take several level exams many times. The data like&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input id $4. examlevel $1. examdate $10. grade $4. times 2;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A 4/20/2013 Fail 3&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A&amp;nbsp; 2/23/2014&amp;nbsp; Fail 5&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A&amp;nbsp; 5/18/2015&amp;nbsp; Pass&amp;nbsp;8&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B&amp;nbsp; 10/12/2013 Fail 2&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 6/20/2014&amp;nbsp; Fail&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 7/10/2015&amp;nbsp; Fail&amp;nbsp;6&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 2/12/2016&amp;nbsp; Pass&amp;nbsp;8&lt;/P&gt;&lt;P&gt;......&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because data are missing or somebody did&amp;nbsp;the wrong calculation. The exam times are not correct. I need to calculation how many times the examee took for each examlevel but I can not change those data before 12/31/2014, i.e.&amp;nbsp; I need the results like belows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;4/20/2013 Fail 3&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A&amp;nbsp; 2/23/2014&amp;nbsp; Fail 5&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; A&amp;nbsp; 5/18/2015&amp;nbsp; Pass&amp;nbsp;6&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B&amp;nbsp; 10/12/2013 Fail 2&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 6/20/2014&amp;nbsp; Fail&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 7/10/2015&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Fail&amp;nbsp;5&lt;/P&gt;&lt;P&gt;id1&amp;nbsp; B 2/12/2016&amp;nbsp; Pass 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;................&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Part of my code is&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=test;&lt;BR /&gt;by id examlevel examdate;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set test;&lt;BR /&gt;by id examlevel;&lt;BR /&gt;if date&amp;lt;=20088(12/31/2014) or first.id and first.examlevel then attempt=times;&lt;BR /&gt;else attempt=lag(times)+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but the code did not work well.&amp;nbsp; Please help me.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443350#M110907</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2018-03-07T15:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to retrieve the previous value to do the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443354#M110908</link>
      <description>&lt;P&gt;You will need to clarify your logic for me.&amp;nbsp; Why does the third row change and where does the 6 come from?&amp;nbsp; Note you can't use lag in this instance because you are changing the lag() value.&amp;nbsp; You would need to use a retained variable, e.g&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lsttimes;
  by id examlevel;
  if first.examlevel then lsttimes=times;
  else do;
    if...then times=lsttimes-2;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:29:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443354#M110908</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-03-07T15:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to retrieve the previous value to do the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443382#M110919</link>
      <description>&lt;P&gt;Thanks for reply. The third row times variable changes because of former employee's mistakes and I can not change the those data because it is in the systems. What I can do is the make the correct times after 2014 based on the times of examees took&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:58:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443382#M110919</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2018-03-07T15:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to retrieve the previous value to do the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443401#M110929</link>
      <description>&lt;P&gt;You want to leave TIMES unchanged for dates &amp;lt;='31dec2014'd, but want to increment times by 1 for all later dates within an ID1/examlevel group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for providing a data step for TEST, but you should have tested it before submitting it to the forum.&amp;nbsp; In particular, reading the examdate as a character variable would defeat any attempt to solve your request without first converting it to a sas date value.&amp;nbsp; There were also error messages generated by the input statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a task where RETAINing the prior value of TIMES is beneficial.&amp;nbsp; I put the retained value in variable _TIMES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input id $4. examlevel :$1. examdate :mmddyy10. grade :$4. times :2.;
datalines;
id1  A 4/20/2013 Fail 3
id1  A  2/23/2014  Fail 5
id1  A  5/18/2015  Pass 8
id1  B  10/12/2013 Fail 2
id1  B 6/20/2014  Fail   4
id1  B 7/10/2015  Fail 6
id1  B 2/12/2016  Pass 8
run;

data want (drop=_:);
  set test;
  by id examlevel;
  retain _times;
  if examdate&amp;lt;='31dec2014'd then _times=times;
  else if first.examlevel=0 then do;
    times=_times+1;
    _times=times;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Mar 2018 16:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443401#M110929</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-07T16:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to retrieve the previous value to do the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443427#M110939</link>
      <description>&lt;P&gt;Thank you very much Mkeintz! That is what I want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 17:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-retrieve-the-previous-value-to-do-the-calculation/m-p/443427#M110939</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2018-03-07T17:28:22Z</dc:date>
    </item>
  </channel>
</rss>

