<?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 add and create a Total Sum from values in a second dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391613#M94105</link>
    <description>&lt;P&gt;Here's a way to approach it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=contract out=costs (drop=_name_);&lt;/P&gt;
&lt;P&gt;var cost;&lt;/P&gt;
&lt;P&gt;id item;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can take a look at the data set COSTS at that point, to get a picture of what is happening.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;if _n_=1 then set costs;&lt;/P&gt;
&lt;P&gt;set invoice;&lt;/P&gt;
&lt;P&gt;select (program);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan1') TotalSum=Bussing + Tutoring;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan2') TotalSum=Clothes + Books + Meals;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan3') TotalSum=Insurance;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; otherwise;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep student TotalSum;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll need to add to the WHEN statements, to define all the plans.&amp;nbsp; And the spelling of the programs ("Plan1", "Plan2") must exactly match the spelling in the INVOICE data set.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Aug 2017 18:00:28 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-08-29T18:00:28Z</dc:date>
    <item>
      <title>How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391606#M94100</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like calculate a total sum for each student based on the program they are on by adding&amp;nbsp;values from a second dataset. &amp;nbsp;Here is the example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;invoice.sas7bdat:&lt;/P&gt;&lt;P&gt;Student &amp;nbsp;Program&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Dave&lt;/TD&gt;&lt;TD&gt;Plan1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Kate&lt;/TD&gt;&lt;TD&gt;Plan2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;Plan3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Emma&lt;/TD&gt;&lt;TD&gt;Plan4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Noah&lt;/TD&gt;&lt;TD&gt;Plan5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;TD&gt;Plan6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;(there are many students, have not listed them all)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Contract.sas7bdat:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Item &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Cost&amp;nbsp; $ &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bussing&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Meals&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Books&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Clothes&lt;/TD&gt;&lt;TD&gt;250&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Tutoring&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Insurance&lt;/TD&gt;&lt;TD&gt;350&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(there are 21 items, have not listed them all)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If a student is on Plan1, then the TotalSum=Bussing+Tutoring&lt;/P&gt;&lt;P&gt;If a student is on Plan2, then the TotalSum=Clothes+Books+Meals&lt;/P&gt;&lt;P&gt;If a studen is on Plan3, then the TotalSum=Insurance&lt;/P&gt;&lt;P&gt;etc......&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create this output:&lt;/P&gt;&lt;P&gt;Student TotalSum&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Dave&lt;/TD&gt;&lt;TD&gt;$400&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Kate&lt;/TD&gt;&lt;TD&gt;$600&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;$350&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;etc...for all students&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this clear, if not please let me know.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 17:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391606#M94100</guid>
      <dc:creator>hypart</dc:creator>
      <dc:date>2017-08-29T17:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391607#M94101</link>
      <description>&lt;P&gt;Can you also provide the plan definitions. You still don't have a way to link these files.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 17:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391607#M94101</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-29T17:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391611#M94103</link>
      <description>&lt;P&gt;The plan definitions is in the section I would like programmed. &amp;nbsp;I'm sorry, I don't know how to link the two files. &amp;nbsp;I have two datasets,&amp;nbsp;&lt;SPAN&gt;invoice.sas7bdat and&amp;nbsp;contract.sas7bdat. &amp;nbsp;That's as far as I've gotten.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 17:51:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391611#M94103</guid>
      <dc:creator>hypart</dc:creator>
      <dc:date>2017-08-29T17:51:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391613#M94105</link>
      <description>&lt;P&gt;Here's a way to approach it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=contract out=costs (drop=_name_);&lt;/P&gt;
&lt;P&gt;var cost;&lt;/P&gt;
&lt;P&gt;id item;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can take a look at the data set COSTS at that point, to get a picture of what is happening.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;if _n_=1 then set costs;&lt;/P&gt;
&lt;P&gt;set invoice;&lt;/P&gt;
&lt;P&gt;select (program);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan1') TotalSum=Bussing + Tutoring;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan2') TotalSum=Clothes + Books + Meals;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('Plan3') TotalSum=Insurance;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; otherwise;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep student TotalSum;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll need to add to the WHEN statements, to define all the plans.&amp;nbsp; And the spelling of the programs ("Plan1", "Plan2") must exactly match the spelling in the INVOICE data set.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 18:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391613#M94105</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-08-29T18:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391614#M94106</link>
      <description>&lt;P&gt;This may give you start:&lt;/P&gt;
&lt;PRE&gt;data contract;
   informat item $15.;
   input item $ cost;
datalines;
Bussing 100 
Meals 150 
Books 200 
Clothes 250 
Tutoring 300 
Insurance 350 
;
run;

proc transpose data=contract out=contrans (drop=_name_);
id item;
var cost;
run;

data contractcosts;
   set contrans;
   Plan1 = sum(bussing,tutoring);
   Plan2 = sum(Clothes,Books,Meals);
   Plan3 = Insurance;
run;
proc transpose data=contractcosts out=costtrans;
var plan: ;
run;

Data invoice;
   input student $ Program $;
datalines;
Dave Plan1 
Kate Plan2 
Mary Plan3 
;
run;

proc sql;
   create table want as
   select invoice.student, costtrans.col1 as total
   from invoice left join costtrans
     on upcase(invoice.program)=upcase(costtrans._name_);
quit;&lt;/PRE&gt;
&lt;P&gt;Since you do not provide any example of what Plans 4, 5, 6 etc may look like I did not attempt them. Add them into the contractcosts following the obvious pattern.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note how to provide data as data step.&lt;/P&gt;
&lt;P&gt;This will not work for any student that has 2 or more "plans" as shown. If you have that case then the "want" set would need to be summed one more time with something like:&lt;/P&gt;
&lt;P&gt;Proc summary data=want nway;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; class student;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var total;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output out=final (drop=_:) sum=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 18:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391614#M94106</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-29T18:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391617#M94107</link>
      <description>&lt;P&gt;I think you need to create the PLAN data set - manually if required.&lt;/P&gt;
&lt;P&gt;How do you know what PLAN1, PLAN2 correspond to? If it's in your head you can create the data set by writing it out.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or IF/THEN but then any changes require changes to your IF/THEN statement rather than updating your PLAN dataset and running the rest of your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how to create it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data plan;
input plan $ service $;
cards;
Plan1 Bus
Plan1 Food
Plan1 House
Plan2 Bus
Plan2 Food
Plan3 Bus
Plan4 Food
Plan5 House
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you build a dataset like that you can then do the merges properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that IF/THEN works fine, just that this approach will make it easier on you if you ever need to repeat this or if you need to extend it. Obviously this is my opinion only.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 18:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391617#M94107</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-29T18:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to add and create a Total Sum from values in a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391652#M94123</link>
      <description>&lt;P&gt;Works! &amp;nbsp;Thanks so much everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 19:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-and-create-a-Total-Sum-from-values-in-a-second/m-p/391652#M94123</guid>
      <dc:creator>hypart</dc:creator>
      <dc:date>2017-08-29T19:45:40Z</dc:date>
    </item>
  </channel>
</rss>

