<?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: Calculating the score average of other firms in the same year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917864#M361559</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*You want this ?*/
data have;
infile cards expandtabs truncover;
input gvkey1	year	score;
cards;
1011	1988	0.1508
1011	1988	0.0851
1011	1988	0.0258
1011	1988	0.0097
1013	1988	0.0215
1013	1988	0.0154
1013	1988	0.0842
1013	1988	0.0006
1034	1988	0.0434
1034	1988	0.0193
1034	1988	0.0324
1036	1988	0.004
1036	1988	0.0393
1036	1988	0.0177
1036	1988	0.0103
1045	1988	0.0712
1045	1988	0.0059
1045	1988	0.0636
1045	1988	0.007
1045	1988	0.0619
1065	1988	0.0144
1065	1988	0.0201
1065	1988	0.0006
1065	1988	0.0101
1072	1988	0.0663
1073	1988	0.0021
1073	1988	0.0462
1073	1988	0.006
;


proc sql;
create table want as
select *,(select mean(score) from have where year=a.year and gvkey1 ne a.gvkey1) as average_score
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 26 Feb 2024 05:21:17 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-02-26T05:21:17Z</dc:date>
    <item>
      <title>Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917825#M361537</link>
      <description>&lt;P&gt;Dear Madam/Sir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to calculate the score average of other firms in the same year. For instance, for gvkey1 # 1011, the average score = score sum (gvkey # 1013, 1034,1036,1045,1065,1072,1073)/7.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any programming help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;gvkey1&lt;/TD&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;score&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1011&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.1508&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1011&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0851&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1011&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0258&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1011&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0097&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1013&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0215&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1013&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0154&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1013&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0842&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1013&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1034&lt;/TD&gt;&lt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TD&gt;&lt;TD&gt;0.0619&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1065&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0144&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1065&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0201&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1065&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1065&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0101&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1072&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0663&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1073&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1073&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.0462&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1073&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;0.006&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;Joon1&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2024 19:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917825#M361537</guid>
      <dc:creator>joon1</dc:creator>
      <dc:date>2024-02-25T19:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917833#M361543</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;For instance, for gvkey1 # 1011, the average score = score sum (gvkey # 1013, 1034,1036,1045,1065,1072,1073)/7.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that the right formula? Or do you mean this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, for gvkey1 # 1011, the average score = score sum (mean(gvkey # 1013), mean(gvkey1 # 1034), mean(gvkey1 # 1036),mean(gvkey1 # 1045),mean(gvkey1 # 1065),mean(gvkey1 # 1072),mean(gvkey1 # 1073)/7.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2024 20:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917833#M361543</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-25T20:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917840#M361546</link>
      <description>&lt;P&gt;Thanks a lot for your prompt reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I meant&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;for gvkey1 # 1011, the average score = score sum of other gvkeys (gvkey # 1013, 1034,1036,1045,1065,1072,1073)/ 24 (the number of all other observations, not 7)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you for your help&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Sincerely,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Joon1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2024 22:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917840#M361546</guid>
      <dc:creator>joon1</dc:creator>
      <dc:date>2024-02-25T22:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917863#M361558</link>
      <description>&lt;P&gt;These program are untested.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data are sorted by year, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each year is read twice, the first time to generate sum of score, and count of scores.&amp;nbsp; The second time to calculate the desired mean.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (in=firstpass)
      have (in=secondpass) ;
  by year;
  if first.year then call missing(_n,_sum);
  if firstpass then do;
    _n+1;
    _sum+score;
  end;
  if secondpass;
  if _n&amp;gt;1 then mean_of_other_gvkeys=(_sum-score)/(_n-1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the data are not sorted, you still have to read data twice, but not year by year.&amp;nbsp; So the hash object is used to store the sum and count during the first pass, and the secondpass to retrieve _N and _SUM for the year-in-hand:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (in=firstpass)
      have (in=secondpass) ;

  if _n_=1 then do;
    call missing(_n,_sum);
    declare hash h ();
      h.definekey('year');
      h.definedata('_n','_sum');
      h.definedone();
  end;
  if firstpass then do;
    if h.find()^=0 then call missing(_n,_sum);
    _n=sum(_n,1);
    _sum=sum(_sum,score);
    h.replace();
  end;
  if secondpass;
  h.find();
  if _n&amp;gt;1 then mean_of_other_gvkeys=(_sum-score)/(_n-1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 05:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917863#M361558</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-26T05:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917864#M361559</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*You want this ?*/
data have;
infile cards expandtabs truncover;
input gvkey1	year	score;
cards;
1011	1988	0.1508
1011	1988	0.0851
1011	1988	0.0258
1011	1988	0.0097
1013	1988	0.0215
1013	1988	0.0154
1013	1988	0.0842
1013	1988	0.0006
1034	1988	0.0434
1034	1988	0.0193
1034	1988	0.0324
1036	1988	0.004
1036	1988	0.0393
1036	1988	0.0177
1036	1988	0.0103
1045	1988	0.0712
1045	1988	0.0059
1045	1988	0.0636
1045	1988	0.007
1045	1988	0.0619
1065	1988	0.0144
1065	1988	0.0201
1065	1988	0.0006
1065	1988	0.0101
1072	1988	0.0663
1073	1988	0.0021
1073	1988	0.0462
1073	1988	0.006
;


proc sql;
create table want as
select *,(select mean(score) from have where year=a.year and gvkey1 ne a.gvkey1) as average_score
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Feb 2024 05:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917864#M361559</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-26T05:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917892#M361575</link>
      <description>&lt;P&gt;This is just math. The average you want is the (sum score of all - sum score of this GVKEY1)/(n of all - n of this GVKEY1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    class year gvkey1;
    var score;
    types year year*gvkey1;
    output out=sums sum=sum n=n;
run;

data want;
    merge sums(where=(_type_=2) drop=_freq_ rename=(sum=total_sum n=total_n))
        sums(where=(_type_=3) drop=_freq_);
    by year;
    avg_of_other_groups=(total_sum-sum)/(total_n-n);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Feb 2024 12:24:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917892#M361575</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-26T12:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917933#M361591</link>
      <description>&lt;P&gt;Thank you so much for your help, PaigeMiller. Have a great week.&lt;/P&gt;&lt;P&gt;Joon1&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 18:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917933#M361591</guid>
      <dc:creator>joon1</dc:creator>
      <dc:date>2024-02-26T18:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the score average of other firms in the same year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917934#M361592</link>
      <description>Thank you for your help, Ksharp. Have a great week.&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Feb 2024 18:48:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-score-average-of-other-firms-in-the-same-year/m-p/917934#M361592</guid>
      <dc:creator>joon1</dc:creator>
      <dc:date>2024-02-26T18:48:49Z</dc:date>
    </item>
  </channel>
</rss>

