<?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 How do I sum down a column in a fashion that is similar to a cumulative frequency? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767272#M243230</link>
    <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on a project, and to my understanding, I need a way to sum values down a column, by an id group: however my variable needs to sum only values from visits that were achieved from dates above it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, say I am a doctor, with data on all of the visits from patients. If a patient needs stitches, I total the number of stitches needed for that visit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="25%" height="57px"&gt;ID&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;visit_order&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;num_stitches&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;total_stitches (to this point)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;and so on&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I write code to get the total_stitches column to work? I have tried various uses of the lag function, but can not get any to work. Thank you so much for your time!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my example data step, in case that may be of use.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input ID $ visit_order num_stitches; 
	DATALINES; 
A	1	5	
A	2	.	
A	3	3	
B	1	2	
B	2	4	
B	3	.	
B	4	12	
C	1	.	
C	2	.	
D	1	.	
D	2	2	
D	3	.	
; 
run; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want; 
	input ID $ visit_order num_stitches total_stitches; 
	DATALINES; 
A	1	5	5
A	2	.	5
A	3	3	8
B	1	2	2
B	2	4	6
B	3	.	6
B	4	12	18
C	1	.	.
C	2	.	.
D	1	.	.
D	2	2	2
D	3	.	2
; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you again for any insight! I greatly appreciate it!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 12 Sep 2021 18:58:41 GMT</pubDate>
    <dc:creator>awardell</dc:creator>
    <dc:date>2021-09-12T18:58:41Z</dc:date>
    <item>
      <title>How do I sum down a column in a fashion that is similar to a cumulative frequency?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767272#M243230</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on a project, and to my understanding, I need a way to sum values down a column, by an id group: however my variable needs to sum only values from visits that were achieved from dates above it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, say I am a doctor, with data on all of the visits from patients. If a patient needs stitches, I total the number of stitches needed for that visit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="25%" height="57px"&gt;ID&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;visit_order&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;num_stitches&lt;/TD&gt;
&lt;TD width="25%" height="57px"&gt;total_stitches (to this point)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;D&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;.&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%" height="30px"&gt;and so on&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I write code to get the total_stitches column to work? I have tried various uses of the lag function, but can not get any to work. Thank you so much for your time!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my example data step, in case that may be of use.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input ID $ visit_order num_stitches; 
	DATALINES; 
A	1	5	
A	2	.	
A	3	3	
B	1	2	
B	2	4	
B	3	.	
B	4	12	
C	1	.	
C	2	.	
D	1	.	
D	2	2	
D	3	.	
; 
run; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want; 
	input ID $ visit_order num_stitches total_stitches; 
	DATALINES; 
A	1	5	5
A	2	.	5
A	3	3	8
B	1	2	2
B	2	4	6
B	3	.	6
B	4	12	18
C	1	.	.
C	2	.	.
D	1	.	.
D	2	2	2
D	3	.	2
; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you again for any insight! I greatly appreciate it!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Sep 2021 18:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767272#M243230</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2021-09-12T18:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sum down a column in a fashion that is similar to a cumulative frequency?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767274#M243232</link>
      <description>&lt;P&gt;Please explain why the 3rd row shows total_stitches=3&lt;/P&gt;</description>
      <pubDate>Sun, 12 Sep 2021 18:51:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767274#M243232</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-12T18:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sum down a column in a fashion that is similar to a cumulative frequency?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767275#M243233</link>
      <description>that is a typo. My apologies. It should be 8</description>
      <pubDate>Sun, 12 Sep 2021 18:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767275#M243233</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2021-09-12T18:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sum down a column in a fashion that is similar to a cumulative frequency?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767277#M243235</link>
      <description>&lt;P&gt;You use a &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_self"&gt;SUM statement&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by id;
    if first.id then total_stitches=0;
    total_stitches+num_stitches;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Sep 2021 18:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767277#M243235</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-12T18:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sum down a column in a fashion that is similar to a cumulative frequency?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767278#M243236</link>
      <description>&lt;P&gt;Just use a SUM statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input ID $ visit_order num_stitches expected; 
DATALINES; 
A	1	5	5
A	2	.	5
A	3	3	8
B	1	2	2
B	2	4	6
B	3	.	6
B	4	12	18
C	1	.	.
C	2	.	.
D	1	.	.
D	2	2	2
D	3	.	2
; 

data want;
  set have;
  by id;
  if first.id then total_stitches=.;
  total_stitches + num_stitches;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why are you setting total_stitches to missing instead of zero when first visit does not have any stitches?&amp;nbsp; If you would rather the total be zero in those cases just change the value that is assigned for the first observation for the patient.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Sep 2021 18:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-sum-down-a-column-in-a-fashion-that-is-similar-to-a/m-p/767278#M243236</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-12T18:59:08Z</dc:date>
    </item>
  </channel>
</rss>

