<?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: Calculate weighted average data from two files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679737#M205278</link>
    <description>&lt;P&gt;Maybe you can use the solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;, although I think simpler code might be available. Nevertheless, allow me to make a few comments in red&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343488"&gt;@luch25&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, because some companies are located in multiple states (example: company D is located in states 5, 6, 7, 8).&lt;BR /&gt;I calculated the weighted average in Excel, but the steps are basically:&lt;BR /&gt;1. Calculate the total population across all states (3000)&amp;nbsp;&lt;FONT color="#FF0000"&gt;no its not, it adds up to 2400&lt;/FONT&gt;&lt;BR /&gt;2. Calculate the state population for each company (e.g., company D has 4 states, with a total population of 1600) &lt;FONT color="#FF0000"&gt;there is no state 5 in the first data set&lt;/FONT&gt;&lt;BR /&gt;3. Multiply the company's unweighted average by the: (value from step 2 / value from step 1). Do this for each company, and then sum across all companies for the weighted average. &lt;FONT color="#FF0000"&gt;Step by step explanation, please ...&lt;/FONT&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Aug 2020 14:28:27 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-08-27T14:28:27Z</dc:date>
    <item>
      <title>Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679701#M205267</link>
      <description>&lt;P&gt;I have two different data sets that I need to use to calculate a weighted average:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Data set with population count for each state&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data state;
input state pop_count;
datalines;
1 200
2 300
3 400
4 500
6 200
7 300
8 500
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;2. Dataset unweighted average for each company and states that they are located in. Some companies are located in multiple states, which is indicated by the colX variables.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data company;
input name $ mean col1 col2 col3 col4;
datalines;
A 0.6 1 . . .
B 0.8 2 3 . .
C 0.7 4 . . .
D 0.5 5 6 7 8
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue that I'm having is how to account for companies located in multiple states. How can I get the state-level total population count data from the state data set into the company data set?&lt;/P&gt;&lt;P&gt;The final weighted average should be 0.61 for the data listed above.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 12:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679701#M205267</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2020-08-27T12:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679717#M205274</link>
      <description>&lt;P&gt;Eight states, but only COL1-COL4? I don't understand.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Can you show us the mathematical calculation steps you follow to get the answer 0.61?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 13:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679717#M205274</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-08-27T13:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679720#M205275</link>
      <description>Yes, because some companies are located in multiple states (example: company D is located in states 5, 6, 7, 8).&lt;BR /&gt;I calculated the weighted average in Excel, but the steps are basically:&lt;BR /&gt;1. Calculate the total population across all states (3000)&lt;BR /&gt;2. Calculate the state population for each company (e.g., company D has 4 states, with a total population of 1600)&lt;BR /&gt;3. Multiply the company's unweighted average by the: (value from step 2 / value from step 1). Do this for each company, and then sum across all companies for the weighted average.</description>
      <pubDate>Thu, 27 Aug 2020 13:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679720#M205275</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2020-08-27T13:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679732#M205277</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343488"&gt;@luch25&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can compute the weighted average with PROC MEANS, so you just need to create an input dataset containing the names, means and weights:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=name mean popc);
array popcnt[50] _temporary_;
if _n_=1 then do until(lr);
  set state end=lr;
  popcnt[state]=pop_count;
end;
set company;
array col[*] col:;
do i=1 to dim(col) while(col[i]); 
  popc=sum(popc,popcnt[col[i]]);
end;
run;

proc means data=want mean;
weight popc;
var mean;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is 0.61 if the missing observation with &lt;FONT face="courier new,courier"&gt;state=5&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;pop_count=600&lt;/FONT&gt;&amp;nbsp;has been inserted into dataset STATE.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 14:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679732#M205277</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-08-27T14:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679737#M205278</link>
      <description>&lt;P&gt;Maybe you can use the solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;, although I think simpler code might be available. Nevertheless, allow me to make a few comments in red&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343488"&gt;@luch25&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, because some companies are located in multiple states (example: company D is located in states 5, 6, 7, 8).&lt;BR /&gt;I calculated the weighted average in Excel, but the steps are basically:&lt;BR /&gt;1. Calculate the total population across all states (3000)&amp;nbsp;&lt;FONT color="#FF0000"&gt;no its not, it adds up to 2400&lt;/FONT&gt;&lt;BR /&gt;2. Calculate the state population for each company (e.g., company D has 4 states, with a total population of 1600) &lt;FONT color="#FF0000"&gt;there is no state 5 in the first data set&lt;/FONT&gt;&lt;BR /&gt;3. Multiply the company's unweighted average by the: (value from step 2 / value from step 1). Do this for each company, and then sum across all companies for the weighted average. &lt;FONT color="#FF0000"&gt;Step by step explanation, please ...&lt;/FONT&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 14:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679737#M205278</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-08-27T14:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679746#M205284</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;this is exactly what I needed, thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Question- what does the "array popcnt[50]" do? Is this basically creating a rule for theoretical number of 50 states? So if I only had 8 states, could this be changed to popcnt[8]? That seemed to be the case when I played around with your code, but want to make sure.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 14:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679746#M205284</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2020-08-27T14:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679760#M205291</link>
      <description>&lt;P&gt;Exactly. The array POPCNT just needs to be large enough so that its index range (here: 1 through 50) includes all values of variable STATE (most importantly the non-missing values of COL1-COL4), so in your example &lt;FONT face="courier new,courier"&gt;popcnt[&lt;STRONG&gt;8&lt;/STRONG&gt;]&lt;/FONT&gt; would be sufficient. (Other possible input datasets may contain non-consecutive state codes like 7, 13, 29, etc. so that not only the number of distinct codes is relevant.) Since it's a temporary array, the difference between 50 and 8 doesn't really matter.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 15:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679760#M205291</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-08-27T15:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate weighted average data from two files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679765#M205294</link>
      <description>Got it, thank you for the explanation and help with the code! Have a great day!</description>
      <pubDate>Thu, 27 Aug 2020 15:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-weighted-average-data-from-two-files/m-p/679765#M205294</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2020-08-27T15:30:50Z</dc:date>
    </item>
  </channel>
</rss>

