<?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: Adding observation value to another observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599275#M172964</link>
    <description>&lt;P&gt;Have you tried the solution provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; ? Afaik it will create the expected dataset - maybe the variables are in ordered differently, but the order of variables is hardly important at all.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 10:26:45 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-10-25T10:26:45Z</dc:date>
    <item>
      <title>Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599030#M172837</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As explicit as my problem may sound haha, it's been really annoying as I'm new to SAS.&lt;/P&gt;
&lt;P&gt;Basically I have a dataset with different types of observations (call them "main" and "sub"), then I have the amount it costs, the transaction id and sub-id.&lt;/P&gt;
&lt;P&gt;So for example, we've got:&lt;/P&gt;
&lt;P&gt;type&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cost&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sub-id&lt;/P&gt;
&lt;P&gt;main&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;sub&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want is this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;type&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cost&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sub-id&amp;nbsp; &amp;nbsp; &amp;nbsp; sub-cost&lt;/P&gt;
&lt;P&gt;main&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there can be more than one pair of main/sub for one id... and sometimes mains are alone but subs are always with a main...&lt;/P&gt;
&lt;P&gt;Thnkassss&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 13:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599030#M172837</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-24T13:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599041#M172841</link>
      <description>&lt;P&gt;It's nearly impossible to provide a generalized solution for you that will work in many situations, as you have provided an example that is really only one situation. If the entire data set mirrors this one example (although I suspect it won't), then this will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    merge have(where=(type='main')) have(where=(type='sub')
       rename=(cost=sub_cost));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also please note that SAS variables cannot have a hyphen in their name, even though your example seems to imply they can have hyphens.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 14:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599041#M172841</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-24T14:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599270#M172961</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've written in SAS code the have and want datasets, do you think you could help with the code that would go in between both pls?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input type $ cost key sub_key;
	cards;
main 15 144 1
sub 13 144 2
main 10 144 3
sub 12 144 4
main 21 129 1
;
run;

data want;
	input type $ cost sub_cost key sub_key;
	cards;
main 15 13 144 1
main 10 12 144 3
main 21 0 129 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 09:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599270#M172961</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-25T09:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599275#M172964</link>
      <description>&lt;P&gt;Have you tried the solution provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; ? Afaik it will create the expected dataset - maybe the variables are in ordered differently, but the order of variables is hardly important at all.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 10:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599275#M172964</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-25T10:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599278#M172966</link>
      <description>&lt;P&gt;This data contains examples that my original code will not properly handle, and this data probably should have been provided originally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since there is no obvious ID variable to merge on, and some "main" records are not followed by "sub" records, I don't have an obvious answer. Also, this remains a very brief example, and so before trying to think about a possible solution, I'd like to know the full set of rules you want to follow.Specifically (but not limited to)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;are there cases where "main" has two (or more) "sub records"? (And what should be done in that case)&lt;/LI&gt;
&lt;LI&gt;are there cases where "main" is followed by another "main"?&lt;/LI&gt;
&lt;LI&gt;do variables cost and key play any role here?&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 25 Oct 2019 11:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599278#M172966</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-25T11:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599312#M172981</link>
      <description>&lt;P&gt;hello again,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;yes i'm sorry, I tried&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; 's code and it works, thanks a lot!!&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 12:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599312#M172981</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-25T12:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599317#M172986</link>
      <description>&lt;P&gt;Hello Again!&lt;/P&gt;
&lt;P&gt;To answer your request, the rules are that:&lt;/P&gt;
&lt;P&gt;-&amp;nbsp; a main can be solo (one observation for the id and no sub)&lt;/P&gt;
&lt;P&gt;- a main can have only one sub and the sub-id of the sub is the main's sub-id + 1&lt;/P&gt;
&lt;P&gt;- there can be two or more mains per id, but each individual one does'nt have a sub&lt;/P&gt;
&lt;P&gt;- subs can't be alone&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that was clear haha&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 13:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599317#M172986</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-25T13:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599318#M172987</link>
      <description>&lt;P&gt;It looks like perhaps you are looking at the problem backwards.&amp;nbsp; You don't want move the SUB values UP to the MAIN line.&amp;nbsp; You want to keep the MAIN values and CARRY them FORWARD onto the SUB lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input type $ cost id sub_id ;
cards;
main 10 123 1
sub 5 123 2
;
data want;
  set have ;
  if type='main' then do;
    main_cost=cost;
    main_id = id;
  end;
  else output;
  retain main_cost main_id ;
  rename cost=sub_cost ;
  drop type;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 13:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599318#M172987</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-25T13:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599369#M173010</link>
      <description>&lt;P&gt;Hello and thanks for the answer,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would this work if there are multiple main/sub for a single id?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good day&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599369#M173010</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-25T15:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: Adding observation value to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599753#M173194</link>
      <description>Hello,&lt;BR /&gt;This method seems to work only with mains that are paired with a main, which means that all mains who don't have a sub are "dropped"...</description>
      <pubDate>Mon, 28 Oct 2019 10:39:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-observation-value-to-another-observation/m-p/599753#M173194</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-28T10:39:48Z</dc:date>
    </item>
  </channel>
</rss>

