<?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 Calculated field based on row values within class variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396450#M278156</link>
    <description>&lt;P&gt;Here's a sample table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;acct&lt;EM&gt; &amp;nbsp; &amp;nbsp;Transcode &amp;nbsp; &amp;nbsp;Amount&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;400 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;110 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;410 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 400&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;1160 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;desired output:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;acct &amp;nbsp; Calc&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; 5000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The goal is to create a new calculated field based not only on information within the row, but also on info in other rows within a class variable (here, Acct). There are many accounts, "Acct" being a unique ID for one. This table houses all transactions taking place within an account. I need to sum up the amounts based on information in other rows of that same acct. For example, subtract amount of TransCode 110 from amount of TransCode 400 &lt;STRONG&gt;only when&lt;/STRONG&gt;&amp;nbsp;amount of TransCode 410 &amp;gt;= amount of TransCode 400. I.e. Take 5000-2000 if (400&amp;gt;=5000) else take 5000.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are millions of unique accounts and about ten other similar conditions. Another example would be to add in the amount for 1160 only if no TransCode 400 is found. All of these additions and subtractions will come together in one field. That one number is then linked to the unique acct number and joined to another table where the acct number is not duplicated. So, this one acct 1234 would be reduced to one row with one value for the new calculated field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. I don't have beginning code because I can't think of a reasonable way to begin. The concept is very doable on paper, but I cannot think of a way to apply this on a grand scale. Well, I can, but it involves many separate table pulls/merges and greatly increasing the number of fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using SAS Enterprise Guide 7.13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 21:13:31 GMT</pubDate>
    <dc:creator>jess11111111</dc:creator>
    <dc:date>2017-09-15T21:13:31Z</dc:date>
    <item>
      <title>Calculated field based on row values within class variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396450#M278156</link>
      <description>&lt;P&gt;Here's a sample table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;acct&lt;EM&gt; &amp;nbsp; &amp;nbsp;Transcode &amp;nbsp; &amp;nbsp;Amount&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;400 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;110 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;410 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 400&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; &amp;nbsp;1160 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;desired output:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;acct &amp;nbsp; Calc&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1234 &amp;nbsp; 5000&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The goal is to create a new calculated field based not only on information within the row, but also on info in other rows within a class variable (here, Acct). There are many accounts, "Acct" being a unique ID for one. This table houses all transactions taking place within an account. I need to sum up the amounts based on information in other rows of that same acct. For example, subtract amount of TransCode 110 from amount of TransCode 400 &lt;STRONG&gt;only when&lt;/STRONG&gt;&amp;nbsp;amount of TransCode 410 &amp;gt;= amount of TransCode 400. I.e. Take 5000-2000 if (400&amp;gt;=5000) else take 5000.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are millions of unique accounts and about ten other similar conditions. Another example would be to add in the amount for 1160 only if no TransCode 400 is found. All of these additions and subtractions will come together in one field. That one number is then linked to the unique acct number and joined to another table where the acct number is not duplicated. So, this one acct 1234 would be reduced to one row with one value for the new calculated field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. I don't have beginning code because I can't think of a reasonable way to begin. The concept is very doable on paper, but I cannot think of a way to apply this on a grand scale. Well, I can, but it involves many separate table pulls/merges and greatly increasing the number of fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using SAS Enterprise Guide 7.13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 21:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396450#M278156</guid>
      <dc:creator>jess11111111</dc:creator>
      <dc:date>2017-09-15T21:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated field based on row values within class variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396466#M278157</link>
      <description>&lt;P&gt;People are hesitant to open unknown attachements these days and your attachment won't open for me in any event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you could post some sample data and the desired result from that data?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 20:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396466#M278157</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-09-15T20:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated field based on row values within class variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396494#M278158</link>
      <description>&lt;P&gt;I do understand the attachment concern. Edited.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 21:14:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396494#M278158</guid>
      <dc:creator>jess11111111</dc:creator>
      <dc:date>2017-09-15T21:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated field based on row values within class variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396530#M278159</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63091"&gt;@jess11111111&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code transposes your data to a single row per account. It stores the transaction amounts in an array of variables with a naming pattern of &lt;EM&gt;amt_&amp;lt;transcode&amp;gt;&lt;/EM&gt;. This will allow you to directly use transaction code specific variable names in your conditions and formulas.&lt;/P&gt;
&lt;P&gt;Having everything in a single row will also give you direct access to all the relevant data for your calculation at once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't fully understood how you're defining your conditions and if this is some sort of a decision tree or just a set of mutually exclusive rules. But having all the data on a single line might already be a step forward for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
 input acct $ Transcode $ Amount;
 datalines;
1234 400 5000
1234 110 2000
1234 410 400
1234 1160 5
5678 400 200
5678 110 100
5678 110 99
;
run;

/* 1 row per account and transaction */
proc sql;
  create view v_sumByTcode as
    select 
      acct,
      transcode,
      sum(amount) as amount
    from have
    group by acct, transcode
  ;
quit;

/* transpose to 1 row per account; variable with amount has transaction code in the name */
proc transpose data=v_sumByTcode out=inter(drop=_:) prefix=amt_;
  by acct;
  id Transcode;
  var Amount;
run;


data want;
  set inter;

  /* only apply rule if account got transactions for all codes involved in rule */
  if nmiss(amt_100, amt_400, amt_410) = 0 then
    do;
      /***
      For example, subtract amount of TransCode 110 from amount of TransCode 400 
      only when amount of TransCode 410 &amp;gt;= amount of TransCode 400. 
      I.e. Take 5000-2000 if (400&amp;gt;=5000) else take 5000. 
      **/
    end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Sep 2017 02:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396530#M278159</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-16T02:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated field based on row values within class variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396683#M278160</link>
      <description>&lt;P&gt;Thanks! This is where my thoughts were going - to get to one row for each account by creating all of those new columns. I was afraid of that necessity because of how many accounts I'll be working with...and many more transcodes/corresponding amounts than I mentioned here conceptually. Processing time is a concern because this will eventually become a daily load.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also yes, this will be based on a bunch of mutually exclusive rules. Anyway, thanks; this definitely helps get my mind wrapped around what I'll have to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2017 19:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculated-field-based-on-row-values-within-class-variable/m-p/396683#M278160</guid>
      <dc:creator>jess11111111</dc:creator>
      <dc:date>2017-09-17T19:18:53Z</dc:date>
    </item>
  </channel>
</rss>

