<?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 Reset a Counter Variable/Calculation Function by ID/Group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436292#M108513</link>
    <description>&lt;P&gt;In addition to the suggestions you have already received, I think you may want to give the problem a little more thought.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you don't include F in your grade format. Shouldn't it get a value of 0 rather than a missing value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And are all grades counted in the calculation of the GPA? Don't some/most schools omit grades like pass, no pass, withdraw, transfer and incomplete?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As such, I think you might want something closer to:&lt;/P&gt;
&lt;PRE&gt;DATA GPAStats (KEEP = TCreditHrs TGradePts GPA STUDENT_ID); 
  SET test.analysts_grades; 
  BY STUDENT_ID; 
  RETAIN TGPACreditHrs 0 TCreditHrs 0 TGradePts 0 GPA 0; 
	
  SELECT(GRADE); 
    WHEN('A') GradePts = 4; 
    WHEN('B') GradePts = 3; 
    WHEN('C') GradePts = 2; 
    WHEN('D') GradePts = 1; 
    WHEN('F') GradePts = 0; 
    OTHERWISE GradePts = .; 
  END; 

  if first.STUDENT_ID then do;
    call missing(TCreditHrs);
    call missing(TGradePts);
    call missing(TGPACreditHrs);
  end;

  TCreditHrs+CREDIT_HR;

  if not missing(GradePts) then do;
    TGPACreditHrs+CREDIT_HR; 
    TGradePts+ (GradePts * CREDIT_HR);
  end;
  
  if last.STUDENT_ID then do;
    GPA = TGradePts / TGPACreditHrs;
    output;
  end;
RUN;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Feb 2018 17:54:41 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2018-02-12T17:54:41Z</dc:date>
    <item>
      <title>How to Reset a Counter Variable/Calculation Function by ID/Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436274#M108507</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to reset a counter variable/calculation function by Student ID in the following example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA GPAStats (KEEP = TCreditHrs TGradePts GPA STUDENT_ID); 
	SET test.analysts_grades; 
	BY STUDENT_ID; 
	RETAIN TCreditHrs 0 TGradePts 0 GPA 0; 
	
	SELECT(GRADE); 
		WHEN('A') GradePts = 4; 
		WHEN('B') GradePts = 3; 
		WHEN('C') GradePts = 2; 
		WHEN('D') GradePts = 1; 
		OTHERWISE GradePts = .; 
	END; 
	
		TCreditHrs = TCreditHrs + CREDIT_HR; 
		TGradePts = TGradePts + (GradePts * CREDIT_HR); 
		GPA = TGradePts / TCreditHrs; 

RUN; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is how the data displays:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ResetCounterByGroup.PNG" style="width: 464px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18461i9AF8458242E2CD83/image-dimensions/464x289?v=v2" width="464" height="289" role="button" title="ResetCounterByGroup.PNG" alt="ResetCounterByGroup.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;As you can see, the functions TCreditHrs, TGradePts and GPA continue to calculate for each Student ID... I would like the functions to reset at the beginning of each Student ID group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Further, I would like to figure out how to select just the last row for each student so that&amp;nbsp;I select just the cumulative total credits, total grade pts, and gpa for all classes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for all the help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 16:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436274#M108507</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-02-12T16:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to Reset a Counter Variable/Calculation Function by ID/Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436275#M108508</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "by XXX" statement creates two automatic variables "first.XXX and last.XXX" :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if first.student_ID then do; ....&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 16:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436275#M108508</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-02-12T16:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to Reset a Counter Variable/Calculation Function by ID/Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436276#M108509</link>
      <description>&lt;P&gt;You can use the FIRST/LAST logic, like below. Make sure to put it before you do the sums.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&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;if first.studentID then do;
tcreditHrs=0;
...


end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you use the SUM function instead of + you can set them to MISSING instead using CALL MISSING.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.studentID then call missing(tcredithrs, tgradepts, gpa);

tcredithrs = sum(tcredithrs, credit_hr)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SUM() considers missing the equivalent of 0, whereas + with a missing value will return a missing value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To keep only the last record, use the LAST, opposite of FIRST.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.studentID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/192948"&gt;@lhsumdalum&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to reset a counter variable/calculation function by Student ID in the following example.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA GPAStats (KEEP = TCreditHrs TGradePts GPA STUDENT_ID); 
	SET test.analysts_grades; 
	BY STUDENT_ID; 
	RETAIN TCreditHrs 0 TGradePts 0 GPA 0; 
	
	SELECT(GRADE); 
		WHEN('A') GradePts = 4; 
		WHEN('B') GradePts = 3; 
		WHEN('C') GradePts = 2; 
		WHEN('D') GradePts = 1; 
		OTHERWISE GradePts = .; 
	END; 
	
		TCreditHrs = TCreditHrs + CREDIT_HR; 
		TGradePts = TGradePts + (GradePts * CREDIT_HR); 
		GPA = TGradePts / TCreditHrs; 

RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is how the data displays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ResetCounterByGroup.PNG" style="width: 464px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18461i9AF8458242E2CD83/image-dimensions/464x289?v=v2" width="464" height="289" role="button" title="ResetCounterByGroup.PNG" alt="ResetCounterByGroup.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;As you can see, the functions TCreditHrs, TGradePts and GPA continue to calculate for each Student ID... I would like the functions to reset at the beginning of each Student ID group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Further, I would like to figure out how to select just the last row for each student so that&amp;nbsp;I select just the cumulative total credits, total grade pts, and gpa for all classes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for all the help.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 16:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436276#M108509</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-12T16:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to Reset a Counter Variable/Calculation Function by ID/Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436292#M108513</link>
      <description>&lt;P&gt;In addition to the suggestions you have already received, I think you may want to give the problem a little more thought.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you don't include F in your grade format. Shouldn't it get a value of 0 rather than a missing value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And are all grades counted in the calculation of the GPA? Don't some/most schools omit grades like pass, no pass, withdraw, transfer and incomplete?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As such, I think you might want something closer to:&lt;/P&gt;
&lt;PRE&gt;DATA GPAStats (KEEP = TCreditHrs TGradePts GPA STUDENT_ID); 
  SET test.analysts_grades; 
  BY STUDENT_ID; 
  RETAIN TGPACreditHrs 0 TCreditHrs 0 TGradePts 0 GPA 0; 
	
  SELECT(GRADE); 
    WHEN('A') GradePts = 4; 
    WHEN('B') GradePts = 3; 
    WHEN('C') GradePts = 2; 
    WHEN('D') GradePts = 1; 
    WHEN('F') GradePts = 0; 
    OTHERWISE GradePts = .; 
  END; 

  if first.STUDENT_ID then do;
    call missing(TCreditHrs);
    call missing(TGradePts);
    call missing(TGPACreditHrs);
  end;

  TCreditHrs+CREDIT_HR;

  if not missing(GradePts) then do;
    TGPACreditHrs+CREDIT_HR; 
    TGradePts+ (GradePts * CREDIT_HR);
  end;
  
  if last.STUDENT_ID then do;
    GPA = TGradePts / TGPACreditHrs;
    output;
  end;
RUN;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 17:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Reset-a-Counter-Variable-Calculation-Function-by-ID-Group/m-p/436292#M108513</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-02-12T17:54:41Z</dc:date>
    </item>
  </channel>
</rss>

