<?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 to create a sum variable and add it to data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528517#M144260</link>
    <description>&lt;P&gt;Hello, first time poster here, been learning SAS over the last few weeks for some research on national health databases. Each row of my data set corresponds to a patient, and there are ~200 columns with lots of different variables about the patient. 15 of these columns correspond to procedures that a patient has undergone, and are named PR1, PR2 ... PR15. The procedures are coded using standardized numbers used in medical coding/billing. For example, 3611 is a heart bypass. I need to create a new variable for each patient that is a sum of all the times a specific procedure is performed. So I need to somehow create a count of the number of times 3611 appears in the patient's 15 procedure columns. I then need to add this result as a new variable to the patient. I appreciate your&amp;nbsp;help!&lt;/P&gt;</description>
    <pubDate>Sat, 19 Jan 2019 13:25:36 GMT</pubDate>
    <dc:creator>rauster</dc:creator>
    <dc:date>2019-01-19T13:25:36Z</dc:date>
    <item>
      <title>How to create a sum variable and add it to data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528517#M144260</link>
      <description>&lt;P&gt;Hello, first time poster here, been learning SAS over the last few weeks for some research on national health databases. Each row of my data set corresponds to a patient, and there are ~200 columns with lots of different variables about the patient. 15 of these columns correspond to procedures that a patient has undergone, and are named PR1, PR2 ... PR15. The procedures are coded using standardized numbers used in medical coding/billing. For example, 3611 is a heart bypass. I need to create a new variable for each patient that is a sum of all the times a specific procedure is performed. So I need to somehow create a count of the number of times 3611 appears in the patient's 15 procedure columns. I then need to add this result as a new variable to the patient. I appreciate your&amp;nbsp;help!&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 13:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528517#M144260</guid>
      <dc:creator>rauster</dc:creator>
      <dc:date>2019-01-19T13:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a sum variable and add it to data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528520#M144262</link>
      <description>&lt;P&gt;To get started, you need to know whether your PR1-PR15 variables are character or numeric.&amp;nbsp; If you don't know, PROC CONTENTS will tell you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since each row corresponds to a patient, the problem is relatively easy.&amp;nbsp; Here's the beginner's way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;count_3611=0;&lt;/P&gt;
&lt;P&gt;if PR1=3611 then count_3611 + 1;&lt;/P&gt;
&lt;P&gt;if PR2=3611 then count_3611 + 1;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;if PR15=3611 then count_3611 + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this assumes that your variables are numeric.&amp;nbsp; If they are actually character, you need to add quotes to the comparisons:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if PR1="3611" then count_3611 + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A more advanced method would use arrays to cut down on the amount of typing needed.&amp;nbsp; This might be something you learn maybe 6 months into learning SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array pr {15} pr1-pr15;&lt;/P&gt;
&lt;P&gt;count_3611 + 1;&lt;/P&gt;
&lt;P&gt;do k=1 to 15;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if pr{k}=3611 then count_3611 + 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop k;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 14:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528520#M144262</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-19T14:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a sum variable and add it to data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528552#M144273</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253897"&gt;@rauster&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello, first time poster here, been learning SAS over the last few weeks for some research on national health databases. Each row of my data set corresponds to a patient, and there are ~200 columns with lots of different variables about the patient. 15 of these columns correspond to procedures that a patient has undergone, and are named PR1, PR2 ... PR15. The procedures are coded using standardized numbers used in medical coding/billing. For example, 3611 is a heart bypass. I need to create a new variable for each patient that is a sum of all the times a specific procedure is performed. So I need to somehow create a count of the number of times 3611 appears in the patient's 15 procedure columns. I then need to add this result as a new variable to the patient. I appreciate your&amp;nbsp;help!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can use WHICHC or WHICHN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your data is sorted by Patient:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

*do this per patient;
by patientID; 

*if first record of a patient, set count to 0;
if first.patient then count=0;

*if any pr1-pr15 = 3611 then increment count;
if whichc('3611', of pr1-pr15) then count+1;

*keep final count per patient;
if last.patientID then output;


run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will only tell you how many per patient, you'll need to merge it back with the main data set.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 18:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528552#M144273</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-19T18:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a sum variable and add it to data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528568#M144282</link>
      <description>&lt;P&gt;This worked perfectly, thanks a lot for your help.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 21:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-sum-variable-and-add-it-to-data-set/m-p/528568#M144282</guid>
      <dc:creator>rauster</dc:creator>
      <dc:date>2019-01-19T21:05:37Z</dc:date>
    </item>
  </channel>
</rss>

