<?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: combine variables based on certain criteria and adjust certain variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544077#M150441</link>
    <description>&lt;P&gt;The code I suggested will not generate that message. Remerging will occur when columns are named in the select clause that are not summarized or mentioned in the group by clause. Can't say more without seeing your code.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Mar 2019 18:43:23 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-03-18T18:43:23Z</dc:date>
    <item>
      <title>combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541532#M149526</link>
      <description>&lt;P&gt;Good evening SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have once again run into a problem which seems pretty complex to me and I have no idea how to solve it. I have also not found anything on this forum that comes close to my problem but it is really hard to put in words so I might've missed it. If this is the case I'm extremely sorry. Once again I'm pretty new to SAS and thus do not have a good understanding of how to properly display my problem here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID; DATE; PERMNO; RETURN; CAPITALIZATION;&lt;/P&gt;&lt;P&gt;1 19990202 a 0.01 400&lt;/P&gt;&lt;P&gt;1 19990202 b 0.04 100&lt;/P&gt;&lt;P&gt;1 19990203 a 0.01 400&lt;/P&gt;&lt;P&gt;1 19990203 b 0.04 100&lt;/P&gt;&lt;P&gt;2 20000101 c 0.01 800&lt;/P&gt;&lt;P&gt;2 20000102 c 0.02 800&lt;/P&gt;&lt;P&gt;3 19300812 d 0.03 750&lt;/P&gt;&lt;P&gt;3 19300813 d 0.03 750&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The main problem is that some of my IDs have multiple permnos for the same date (see observations 1+2 &amp;amp; 3+4).&lt;/P&gt;&lt;P&gt;What I want to happen is that the observations where this is the case are combined to one in the following way:&lt;/P&gt;&lt;P&gt;-ID: stays the same&lt;/P&gt;&lt;P&gt;-Date: stays the same&lt;/P&gt;&lt;P&gt;-Permno: it doesn't really matter which one is taken, either the first or the second one, whatever is easier I guess&lt;/P&gt;&lt;P&gt;-Return: Now here comes the funny part... &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; what I want to happen is: A new return is calculated from the old ones in the following way (a.return*(a.capitalization/(a.capitalization+b.capitalization))+b.return*(b.capitalization/(a.capitalization+b.capitalization)))&lt;/P&gt;&lt;P&gt;-Capitalization: take the sum of the two capitalization (a.capitalization+b.capitalization)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a basic idea on how to identify the specific observations where this problem is apparent. I thought of something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if ID=lag(ID) AND date=lag(date) AND permno^=lag(permno)&lt;/P&gt;&lt;P&gt;then...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and now I do not know how to go on. I have no idea how to properly combine two observations, especially including the calculation of a new return.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance for helping me out. SAS can be so hard to understand as a newbie sometimes..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a great evening.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS. I'm running on SAS 9.4&lt;/P&gt;</description>
      <pubDate>Fri, 08 Mar 2019 18:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541532#M149526</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-08T18:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541543#M149532</link>
      <description>&lt;P&gt;would&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;multiple permnos&amp;nbsp; be sets of two or&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;can it be any number of distinct values a,b,c, and so on ?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Mar 2019 19:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541543#M149532</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-03-08T19:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541666#M149590</link>
      <description>&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID DATE:yymmdd8. PERMNO $ RETURN CAPITALIZATION;
format date yymmdd10.;
datalines;
1 19990202 a 0.01 400
1 19990202 b 0.04 100
1 19990203 a 0.01 400
1 19990203 b 0.04 100
2 20000101 c 0.01 800
2 20000102 c 0.02 800
3 19300812 d 0.03 750
3 19300813 d 0.03 750
;

proc sql;
create table want as
select
    id,
    date,
    min(permNo) as permNo,
    sum(return*capitalization)/ sum(capitalization) as return,
    sum(capitalization) as capitalization
from have
group by id, date;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                    ID        DATE  permNo      return  capitalization
              --------------------------------------------------------
                     1  1999-02-02  a            0.016             500
                     1  1999-02-03  a            0.016             500
                     2  2000-01-01  c             0.01             800
                     2  2000-01-02  c             0.02             800
                     3  1930-08-12  d             0.03             750
                     3  1930-08-13  d             0.03             750
&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Mar 2019 04:16:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541666#M149590</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-09T04:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541706#M149610</link>
      <description>Amazing, thank you very much! Have a great weekend.&lt;BR /&gt;&lt;BR /&gt;BR&lt;BR /&gt;Nici</description>
      <pubDate>Sat, 09 Mar 2019 17:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541706#M149610</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-09T17:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541708#M149612</link>
      <description>PGstats already provided a sufficient solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;I still want to thank you very much for your answer and your time looking through my problem.&lt;BR /&gt;&lt;BR /&gt;BR&lt;BR /&gt;Nici&lt;BR /&gt;</description>
      <pubDate>Sat, 09 Mar 2019 17:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/541708#M149612</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-09T17:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544069#M150438</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been using your code successfully. Somehow I'm having problems now however. Before, as shown in your example, I received only one combined observation. Now the observations are still combined, however, I receive duplicates. If 2 observations where combined, I get the combined observation twice. Why is this the case? I'm really confused because the code worked perfectly before.&lt;/P&gt;&lt;P&gt;I also noticed the following message in the log: "The query requires remerging summary statistics back with the original data"&lt;/P&gt;&lt;P&gt;instead of this output&lt;/P&gt;&lt;PRE&gt;                    ID        DATE  permNo      return  capitalization
              --------------------------------------------------------
                     1  1999-02-02  a            0.016             500
                     1  1999-02-03  a            0.016             500
                     2  2000-01-01  c             0.01             800
                     2  2000-01-02  c             0.02             800
                     3  1930-08-12  d             0.03             750
                     3  1930-08-13  d             0.03             750&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I get the following one:&lt;/P&gt;&lt;PRE&gt;                    ID        DATE  permNo      return  capitalization
              --------------------------------------------------------
                     1  1999-02-02  a            0.016             500&lt;BR /&gt;                     1  1999-02-02  a            0.016             500
                     1  1999-02-03  a            0.016             500&lt;BR /&gt;                     1  1999-02-03  a            0.016             500
                     2  2000-01-01  c             0.01             800
                     2  2000-01-02  c             0.02             800
                     3  1930-08-12  d             0.03             750
                     3  1930-08-13  d             0.03             750&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2019 18:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544069#M150438</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-18T18:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544077#M150441</link>
      <description>&lt;P&gt;The code I suggested will not generate that message. Remerging will occur when columns are named in the select clause that are not summarized or mentioned in the group by clause. Can't say more without seeing your code.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2019 18:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544077#M150441</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-18T18:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544198#M150487</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I might know what the problem is then. The shown here was only a simplification of the actual data. This is how my data looks:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input pcusip date permno ret cap anncmt code shrout prc;&lt;BR /&gt;datalines;&lt;BR /&gt;1 19990202 a 0.01 400 19990209 3 100 4&lt;BR /&gt;1 19990202 b 0.04 100 19990209 3 100 1&lt;BR /&gt;1 19990203 a 0.01 400 19990209 3 100 4&lt;BR /&gt;1 19990203 b 0.04 100 19990209 3 100 1&lt;BR /&gt;2 20000101 c 0.01 800 20000310 3 400 2&lt;BR /&gt;2 20000102 c 0.02 800 20000310 3 400 2&lt;BR /&gt;3 19300812 d 0.03 750 19300810 3 750 1&lt;BR /&gt;3 19300813 d 0.03 750 19300810 3 750 1&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My Code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql; /*Capital weight the returns of same CUSIP with multiple permnos on same date and combine the permnos to one*/&lt;BR /&gt;create table want as&lt;BR /&gt;select&lt;BR /&gt;pcusip,&lt;BR /&gt;date,&lt;BR /&gt;code,&lt;BR /&gt;anncmt,&lt;BR /&gt;sum(shrout) as shrout,&lt;BR /&gt;sum(prc) as prc,&lt;BR /&gt;min(permno) as permno, /*for the two cases in this sample the smaller permno is the one that existed first*/&lt;BR /&gt;sum(ret*cap)/ sum(cap) as ret,&lt;BR /&gt;sum(cap) as cap&lt;BR /&gt;from have&lt;BR /&gt;group by pcusip, date;&lt;BR /&gt;select * from want;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From your comment I guess the problem are the variables anncmt &amp;amp; code right? Since they are not part of any calculations but also are not in the group by statement. Is there any way around this problem?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help once again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;nici&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2019 10:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544198#M150487</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-19T10:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: combine variables based on certain criteria and adjust certain variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544243#M150499</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;Alright, I fixed it. Thank you again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have an amazing day&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;nici&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2019 14:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-variables-based-on-certain-criteria-and-adjust-certain/m-p/544243#M150499</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-19T14:10:04Z</dc:date>
    </item>
  </channel>
</rss>

