As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Tips:Between and Within Group Counters"

From sasCommunity
Jump to: navigation, search
m (minor edits)
(gardening)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Given a sorted data set, adding within and between group counters can be accomplished without the use of any IF statement logic.
+
Given a sorted data set, adding within and between group counters can be accomplished without the use of any [[IF statement]] logic.
 
<source lang="sas">
 
<source lang="sas">
 
data x;
 
data x;
input id score @@;
+
    input id score @@;
datalines;  
+
    datalines;  
 
100 10 100 13 100 16 101 13 101 99 102 20 102 22 102 24 102 26
 
100 10 100 13 100 16 101 13 101 99 102 20 102 22 102 24 102 26
 
;
 
;
 
+
</source>
 +
<table><tr>
 +
<td valign="top">
 +
<source lang="sas">
 
data x;
 
data x;
set x;
+
    set x;
by id;
+
    by id;
between + first.id;
+
    between + first.id;
within + (-first.id * within)+ 1;
+
    within + 1 - first.id * within;
 
run;
 
run;
</source>
+
</source></td>
<pre>
+
<td valign="top"><pre>
 
Obs    id    score    between    within
 
Obs    id    score    between    within
 
  1    100      10        1          1
 
  1    100      10        1          1
Line 25: Line 28:
 
  8    103      24        3          3
 
  8    103      24        3          3
 
  9    103      26        3          4
 
  9    103      26        3          4
</pre>
+
</pre></td></tr></table>
Posted with thanks to [http://support.sas.com/publishing/authors/schreier.html Howard Schreier] for the BETWEEN group idea (in a SAS-L posting).
+
Posted with thanks to [http://support.sas.com/publishing/authors/schreier.html Howard Schreier] for the BETWEEN group idea (in a [[SAS-L]] posting).
<noinclude>
+
 
[[Category:DATA Step]]
+
[[Category:BY Statement]]
+
</noinclude>
+
 
{{ExternalReadMore|http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000761931.htm}}
 
{{ExternalReadMore|http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000761931.htm}}
  
Line 36: Line 36:
 
<div style="float:right">Submitted By [[User:Msz03|Mike Zdeb]]</div>
 
<div style="float:right">Submitted By [[User:Msz03|Mike Zdeb]]</div>
  
[[Category:Tip to be Reviewed]]
+
<noinclude>
 +
[[Category:DATA Step]]
 +
[[Category:BY Statement]]
 +
[[Category:Tip in Use]]
 +
</noinclude>

Latest revision as of 17:18, 8 February 2017

Given a sorted data set, adding within and between group counters can be accomplished without the use of any IF statement logic.

data x;
    input id score @@;
    datalines; 
100 10 100 13 100 16 101 13 101 99 102 20 102 22 102 24 102 26
;
data x;
    set x;
    by id;
    between + first.id;
    within  + 1 - first.id * within;
run;
Obs     id    score    between    within
 1     100      10        1          1
 2     100      13        1          2
 3     100      16        1          3
 4     101      13        2          1
 5     101      99        2          2
 6     103      20        3          1
 7     103      22        3          2
 8     103      24        3          3
 9     103      26        3          4

Posted with thanks to Howard Schreier for the BETWEEN group idea (in a SAS-L posting).

....see also


Submitted By Mike Zdeb