<?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: Add additional variables and assign the values from another rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500289#M133213</link>
    <description>&lt;P&gt;SQL may not be the best solution for this problem.&amp;nbsp; PROC TRANSPOSE can do this more easily.&lt;/P&gt;</description>
    <pubDate>Mon, 01 Oct 2018 09:39:09 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2018-10-01T09:39:09Z</dc:date>
    <item>
      <title>Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500285#M133211</link>
      <description>&lt;P&gt;I am trying to create another 2 variables in my table using PROC SQL statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table as following;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Month ID bonus&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 101 1000&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 102 1050&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 103 1020&lt;/P&gt;&lt;P&gt;201702&amp;nbsp; 101 1550&lt;/P&gt;&lt;P&gt;201702&amp;nbsp; 102 1560&lt;/P&gt;&lt;P&gt;201702&amp;nbsp; 103 1580&lt;/P&gt;&lt;P&gt;201703&amp;nbsp; 101 1750&lt;/P&gt;&lt;P&gt;201703&amp;nbsp; 102 7050&lt;/P&gt;&lt;P&gt;201703&amp;nbsp; 103 1600&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I need 2 more additional columns for each row in which I wish to have the bonus value from my next month and 2nd next month (Bonus, Bonus for next month and Bonus for 2nd next month in a single row). And I am trying to convert the above table as following;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Month ID Bonus Bonus+1 Bonus+2&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 101 1000 &amp;nbsp;1550 &amp;nbsp;1750&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 102 1050 &amp;nbsp;1560 7050&lt;/P&gt;&lt;P&gt;201701&amp;nbsp; 103 1020 &amp;nbsp;1580 1600&lt;/P&gt;&lt;P&gt;&amp;nbsp;...&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;After going through several forums, I could not find&amp;nbsp; way to do it. Can anyone knows the efficient way to do it?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 09:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500285#M133211</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-01T09:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500288#M133212</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input month id bonus;
    cards;
201701 101 1000
201701 102 1050
201701 103 1020
201702 101 1550
201702 102 1560
201702 103 1580
201703 101 1750
201703 102 7050
201703 103 1600    
;
run;

proc sql noprint;
    CREATE TABLE want AS
    SELECT a.*, b.bonus AS bonus1, c.bonus AS bonus2
    FROM have a
    LEFT JOIN have b
    ON b.ID=a.ID AND b.Month=a.Month+1 
    LEFT JOIN have c
    ON c.ID=a.ID AND c.Month=a.Month+2
    ORDER BY Month, ID 
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Oct 2018 09:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500288#M133212</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-10-01T09:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500289#M133213</link>
      <description>&lt;P&gt;SQL may not be the best solution for this problem.&amp;nbsp; PROC TRANSPOSE can do this more easily.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 09:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500289#M133213</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-10-01T09:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500296#M133214</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input month : yymmn6. id bonus;
    format month yymmn6.;
    cards;
201701 101 1000
201701 102 1050
201701 103 1020
201702 101 1550
201702 102 1560
201702 103 1580
201703 101 1750
201703 102 7050
201703 103 1600    
;
run;

proc sql;
select  *,
(select bonus from have where id=a.id and month=intnx('month',a.month,1)) as bonuse1, 
(select bonus from have where id=a.id and month=intnx('month',a.month,2)) as bonuse2
 from have as a;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Oct 2018 10:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500296#M133214</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-10-01T10:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500311#M133217</link>
      <description>&lt;P&gt;Thank you so much for your prompt reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to handle the month when it is 201711 or 201712 (where the month falls as Nov or Dec?). Please note that the month values is in the numeric format.&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 11:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500311#M133217</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-01T11:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500321#M133222</link>
      <description>&lt;P&gt;Use a date format and intnx function as in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s answer.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 12:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500321#M133222</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-10-01T12:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500335#M133226</link>
      <description>&lt;P&gt;Or modify the increments as follows:&lt;/P&gt;
&lt;PRE&gt;... =a.Month+1&lt;FONT color="#FF6600"&gt;+88*(mod(a.Month,100)&amp;gt;11)&lt;/FONT&gt;
...
... =a.Month+2&lt;FONT color="#FF6600"&gt;+88*(mod(a.Month,100)&amp;gt;10)&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Oct 2018 12:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/500335#M133226</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-01T12:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502330#M134085</link>
      <description>&lt;P&gt;Thank you so much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 09:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502330#M134085</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-08T09:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502366#M134103</link>
      <description>&lt;P&gt;Dear I understood that it works. However can you please explain how does the following work;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;=a.Month+2+88*(mod(a.Month,100)&amp;gt;10)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For example;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wish to add 2 months to 201711 and it should output 201801.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reminder for mod(201711,100) is 11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And 11&amp;gt;10 is TRUE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then how it interprets is something I could not get in line with the output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 12:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502366#M134103</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-08T12:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Add additional variables and assign the values from another rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502405#M134112</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/216488"&gt;@ggfggrr&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear I understood that it works. However can you please explain how does the following work;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;=a.Month+2+88*(mod(a.Month,100)&amp;gt;10)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For example;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wish to add 2 months to 201711 and it should output 201801.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reminder for mod(201711,100) is 11.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And 11&amp;gt;10 is TRUE&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;... and the Boolean value TRUE is interpreted as the numeric value 1 in arithmetic expressions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hence, in your example you obtain&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;=201711+2+88*1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which equals 201801, as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The general idea is to save an IF/THEN statement, IFN function call or (in PROC SQL) a CASE expression by using a Boolean expression in a calculation. In this context the Boolean expression evaluates to 1 for TRUE and 0 for FALSE and this difference can be used to switch between two numeric results. To apply this technique, you only have to develop&amp;nbsp;an arithmetic expression (containing the Boolean expression) whose results switch between the two results you want to obtain depending on the Boolean value. The resulting SAS code is mostly concise, but many people will find the longer code (using IF/THEN etc.) more readable and easier to maintain. So, it's a technique rather for mathematically inclined programmers (and "code poets" &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;).&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 13:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-additional-variables-and-assign-the-values-from-another-rows/m-p/502405#M134112</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-08T13:53:23Z</dc:date>
    </item>
  </channel>
</rss>

