<?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: Create a new calculated column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670690#M201366</link>
    <description>&lt;P&gt;You can use the next code to create the table you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select FROM_ID, TO_ID, FROM_BANK,(case when sum(flag)&amp;gt;0 then 100 
								 else FROM_BANK end) as  FROM_BANK_NEW

from	(select FROM_ID, TO_ID, FROM_BANK, sum(from_bank=100) as flag
		from rawtbl
		group by from_id)

group by from_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;The subquery creates a flag to identify the transactions that involve bank 100,&lt;/P&gt;
&lt;P&gt;if the sum of this flag is greater than 0 it is due to the bank 100 was involved at least once.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jul 2020 15:07:28 GMT</pubDate>
    <dc:creator>Angel_Larrion</dc:creator>
    <dc:date>2020-07-20T15:07:28Z</dc:date>
    <item>
      <title>Create a new calculated column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670424#M201252</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have raw data of transactions with following columns:&lt;/P&gt;
&lt;P&gt;FROM_ID (ID of the employer that pay wage)&lt;/P&gt;
&lt;P&gt;TO_ID&amp;nbsp; &amp;nbsp; &amp;nbsp; (ID of the Employee that receive wage)&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;From_Bank (Bank where the money transferred from)&lt;/P&gt;
&lt;P&gt;I want to create a new column called From_Bank_New that will get the following value:&lt;/P&gt;
&lt;P&gt;IF employer&amp;nbsp;&amp;nbsp;has any transaction from bank 100&amp;nbsp; then in all transactions of&amp;nbsp; this employer&amp;nbsp;From_Bank_New will equal 100.&lt;/P&gt;
&lt;P&gt;IF&amp;nbsp;employer doesn't have any transaction from bank 100 then&amp;nbsp;From_Bank_New will equal to the value of column&amp;nbsp;From_Bank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to calculate the new column&amp;nbsp;&lt;CODE class="  language-sas"&gt;FROM_BANK_NEW?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data RawTbl;
Input FROM_ID TO_ID FROM_BANK;
cards;
999 1 100
999 2 100
999 3 100
999 4 120
888 5 80
888 6 80
777 7 100
777 8 100
777 9 100
666 10 90
666 11 50
666 12 100
;
run;



Data wanted;
Input FROM_ID TO_ID FROM_BANK FROM_BANK_New;
cards;
999 1 100 100
999 2 100 100
999 3 100 100
999 4 120 100
888 5 80 80
888 6 70 70
777 7 100 100
777 8 100 100
777 9 100 100
666 10 90 100
666 11 50 100
666 12 100 100
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jul 2020 04:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670424#M201252</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-19T04:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new calculated column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670434#M201259</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort dat=have;
by from_id;
run;

data want;
merge
  have
  have (
    keep=from_id from_bank
    rename=(from_bank=from_bank_new)
    where=(from_bank_new = "100")
    in=in100
  )
;
by from_id;
if not in100 then from_bank_new = from_bank;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;untested, posted from my tablet. In case of problems, please post complete log of the step.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jul 2020 07:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670434#M201259</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-19T07:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new calculated column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670454#M201268</link>
      <description>&lt;P&gt;Why do you want&lt;/P&gt;
&lt;PRE&gt;888 5 80 80
888 6 70 70           &amp;lt;--- where did 70 come from&lt;/PRE&gt;
&lt;P&gt;when you have&lt;/P&gt;
&lt;PRE&gt;888 5 80 80
888 6 80 80&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jul 2020 14:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670454#M201268</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-07-19T14:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new calculated column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670455#M201269</link>
      <description>&lt;P&gt;You can use a DOW loop to compute a flag variable for the group and apply the flag in logic during output in a second DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;Presume BY rows are contiguous albeit disordered.&lt;/P&gt;
&lt;PRE&gt;data want;
  do _n_ = 1 by 1 until (last.from_id);
    set have;
    by from_id notsorted;  * presume contiguous;
&lt;BR /&gt;    * compute flag;
    if not flag_bank_100 then
      flag_bank_100 = from_bank eq 100;
  end;

  do _n_ = 1 to _n_;
    set have;
&lt;BR /&gt;    * apply flag in logic;
    if flag_bank_100 then 
      from_bank_new = 100;
    else
      from_bank_new = from_bank;

    OUTPUT;
  end;

  drop flag_bank_100;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jul 2020 14:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670455#M201269</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-07-19T14:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new calculated column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670690#M201366</link>
      <description>&lt;P&gt;You can use the next code to create the table you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select FROM_ID, TO_ID, FROM_BANK,(case when sum(flag)&amp;gt;0 then 100 
								 else FROM_BANK end) as  FROM_BANK_NEW

from	(select FROM_ID, TO_ID, FROM_BANK, sum(from_bank=100) as flag
		from rawtbl
		group by from_id)

group by from_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;The subquery creates a flag to identify the transactions that involve bank 100,&lt;/P&gt;
&lt;P&gt;if the sum of this flag is greater than 0 it is due to the bank 100 was involved at least once.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jul 2020 15:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-calculated-column/m-p/670690#M201366</guid>
      <dc:creator>Angel_Larrion</dc:creator>
      <dc:date>2020-07-20T15:07:28Z</dc:date>
    </item>
  </channel>
</rss>

