<?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 value from variable of one observation to another observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635901#M188854</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt; thanks so much! That worked well.</description>
    <pubDate>Mon, 30 Mar 2020 17:47:50 GMT</pubDate>
    <dc:creator>wernie</dc:creator>
    <dc:date>2020-03-30T17:47:50Z</dc:date>
    <item>
      <title>How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635415#M188660</link>
      <description>&lt;P&gt;Trying to post this for the third time. Hoping the text shows...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to add the values from one variable for one observation to another observation. I have the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FIPS5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;char_state&amp;nbsp; &amp;nbsp; char_county&amp;nbsp; &amp;nbsp; &amp;nbsp; yr&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;num_days&lt;/P&gt;
&lt;P&gt;51015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;51017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;51019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;51021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;....&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;..&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;....&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . (continued with all FIPS codes)&lt;/P&gt;
&lt;P&gt;51515&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 515&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&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;I want to add the num_days value from 51515 (2) to the num_days value for 51019 (3) and get the new total of 5 assigned to the 51019 row and delete 51515, so I'd end up with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FIPS5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; char_state&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;char_county&amp;nbsp; &amp;nbsp; &amp;nbsp;yr&amp;nbsp; &amp;nbsp; &amp;nbsp; num_days&lt;/P&gt;
&lt;P&gt;51015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;51017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;51019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5 (new total)&lt;/P&gt;
&lt;P&gt;51021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;51&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;....&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ..&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; (continued with all FIPS codes)&lt;/P&gt;
&lt;P&gt;51515 deleted from the table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 19:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635415#M188660</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2020-03-27T19:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635421#M188661</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44533"&gt;@wernie&lt;/a&gt;&amp;nbsp; Are you sure there is no content that follows the question? For example, a data sample of what you HAVE and what you WANT briefly explaning the business logic/requirement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or, if I am mistaken, is it just a casual theoretical question?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 19:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635421#M188661</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-27T19:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635427#M188664</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; ugh, yes, there was much more to that post. I already tried posting it twice and something doesn't seem to be working properly. Strange because it showed up for me initially, but now it's blank.&lt;BR /&gt;&lt;BR /&gt;Will try again in a different browser.</description>
      <pubDate>Fri, 27 Mar 2020 19:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635427#M188664</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2020-03-27T19:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635443#M188672</link>
      <description>&lt;P&gt;And what is the rule that connects these two observations?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 20:44:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635443#M188672</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-27T20:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635447#M188675</link>
      <description>&lt;P&gt;It really helps to provide data in the form of a data step so we can test code. Paste code and log entries into a code box opened on the forum with the &amp;lt;/&amp;gt; or running man icons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way which may be flexible to add other improbable FIPS codes.&lt;/P&gt;
&lt;PRE&gt;
data have;
  input FIPS5   $    char_state $   char_county $     yr       num_days ;
datalines;
51015          51                015                2010          2
51017          51                017                2010          1
51019          51                019                2010          3
51021          51                021                2010          5
51515          51                515                2010          2
;

proc format library=work;
value $fips5_
'51515' = '51019'
;
value $char_county
'515'='019'
;
run;

proc summary data=have nway;
	class fips5 char_state char_county yr;
	format fips5 $fips5_. char_county $char_county.;
	var num_days;
	output out=work.want (drop= _:) sum= ;
run;&lt;/PRE&gt;
&lt;P&gt;You don't say if there are other variables involved. That might take creating a different summary and/or merging the results back onto the data.&lt;/P&gt;
&lt;P&gt;Or you could have used a data step with if/then/else to assign the proper codes and then summarized.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Groups created by formats will be honored by most procedures. A side effect of a format + class statement is that the lowest value of the variable ends up in the output data. So since that is apparently what you wanted this works. Other combinations you may be better off with the data step if/then/else assigning the codes.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 21:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635447#M188675</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-03-27T21:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635566#M188732</link>
      <description>&lt;P&gt;You can do that as shown in the below code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* sort data in descending FIPS5 order */
proc sort data=A out=B;
   by descending FIPS5;
run;

/* calculate your sum */
data C;
   set B;
   retain TEMP;
   if FIPS5='51515' then
   do; /* remember num_days and _n_ for this observation */
      TEMP = num_days;
      call symputx('NDEL',_n_);
   end;
   if FIPS5='51019' then num_days = num_days + TEMP;
run;

/* delete observation */
data C1;
   set C;
   if _n_=&amp;amp;NDEL then delete;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This solves the problem as you described it. If you need to generalize it you need to provide more general requirements. Or having this code example you might be able to figure it out yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more on doing calculations across different SAS data set observations see my blog post:&lt;/P&gt;
&lt;P&gt;&lt;A title="calculations across observations in a SAS data table" href="https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-observations/" target="_self"&gt;&lt;FONT size="4"&gt;&lt;FONT size="3"&gt;Hopping for the best - calculations across SAS dataset observations&lt;/FONT&gt;&lt;/FONT&gt;&lt;/A&gt;&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>Sun, 29 Mar 2020 00:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635566#M188732</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-03-29T00:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to add value from variable of one observation to another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635901#M188854</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt; thanks so much! That worked well.</description>
      <pubDate>Mon, 30 Mar 2020 17:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-value-from-variable-of-one-observation-to-another/m-p/635901#M188854</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2020-03-30T17:47:50Z</dc:date>
    </item>
  </channel>
</rss>

