<?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 dynamic logic based on data available in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559851#M156403</link>
    <description>&lt;P&gt;Transpose the ratio_: variables to a long dataset, sort it, and apply your logic to the sequence. Then the code is completely data-driven.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See Maxim 33.&lt;/P&gt;</description>
    <pubDate>Sat, 18 May 2019 06:35:46 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-05-18T06:35:46Z</dc:date>
    <item>
      <title>Create a dynamic logic based on data available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559836#M156396</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;I have situation where i need to constantly update my code each time the data changes ( added or removed). I'm thinking to build a dynamic logic that get contracted based on data available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example: I have a dataset similar the one below. Here is what i want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. order the ratio column alphabetically [A-Z]. in this case the order will be [ratio_c1, ratio_d1, ratio_d2]&lt;/P&gt;
&lt;P&gt;2. using the order calculate a new column using the below logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;new_ratio_c1 = (balance * pbal)* ratio_c1&lt;/P&gt;
&lt;P&gt;new_ratio_d1 = (balance * pbal)* ratio_d1 + pbal * 1 - ratio_c1 - ratio_d1&lt;/P&gt;
&lt;P&gt;new_ratio_d2 = (balance * pbal) * ratio_d2 + pbal * 1 - ratio_c1 - ratio_d1 - ratio_d2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic need to update itself if there are a new ratio. for example, there was ratio_d3&amp;nbsp;&lt;/P&gt;
&lt;P&gt;new_ratio_d3 = (balance * pbal) * ratio_d3 + pbal * 1 - ratio_c1 - ratio_d1 - ratio_d2 - ratio_d3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input group $ month balance pbal ratio_d2 ratio_d1 ratio_c1;
datalines; 
A 0 2050 100 .002 .051 .001
A 1 2030 100 .002 .021 .092
A 2 2010 100 .004 .022 .001
B 0 1500 400 .002 .042 .091
B 1 1450 400 .012 .132 .061
B 2 1430 400 .015 .232 .051
B 3 1420 400 .129 .023 .051
C 0 2300 300 .165 .002 .021
C 1 2350 300 .098 .026 .041
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really appreciate your kind help and suggestions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 May 2019 02:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559836#M156396</guid>
      <dc:creator>zqkal</dc:creator>
      <dc:date>2019-05-18T02:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create a dynamic logic based on data available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559841#M156397</link>
      <description>&lt;P&gt;Here's a starting point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct name into : varlist
from dictionary.columns where
memname = 'HAVE' and
libname = 'WORK'
and upcase(name) =: 'RATIO_';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You probably need to check the syntax on these tools (is UPCASE needed in other places, will =: work&amp;nbsp; in SQL).&amp;nbsp; But the trick is that SELECT DISTINCT will automatically alphabetize values in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From there, the rest is not too bad:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro final;

data want;
   set have;
   array vars {&amp;amp;sqlobs} &amp;amp;varlist;
.......

%mend final;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I don't have time to work on the rest of it, but it should be straightforward as you run through the list of words within &amp;amp;VARLIST in a macro loop.&lt;/P&gt;</description>
      <pubDate>Sat, 18 May 2019 03:32:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559841#M156397</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-18T03:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a dynamic logic based on data available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559843#M156398</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45150"&gt;@zqkal&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does below return what you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input group $ month balance pbal ratio_d2 ratio_d1 ratio_c1;
datalines; 
A 0 2050 100 .002 .051 .001
A 1 2030 100 .002 .021 .092
A 2 2010 100 .004 .022 .001
B 0 1500 400 .002 .042 .091
B 1 1450 400 .012 .132 .061
B 2 1430 400 .015 .232 .051
B 3 1420 400 .129 .023 .051
C 0 2300 300 .165 .002 .021
C 1 2350 300 .098 .026 .041
;
run;

%let source_vars=;
%let target_vars=;
proc sql noprint;
  select 
    name,
    cats('new_',name)
      into 
        :source_vars separated by ' ',
        :target_vars separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
  and upcase(name) like 'RATIO^_%' escape '^'
  order by upcase(name)
  ;
quit;

data want(drop=_:);
  set have;
  array source_var {*} &amp;amp;source_vars;
  array target_var {*} &amp;amp;target_vars;

  target_var[1]=balance * pbal * source_var[1];
  _sum01=source_var[1];
  do _i=2 to dim(target_var);
    _sum01=_sum01+source_var[_i];
    target_var[_i]=balance * pbal * source_var[_i] + pbal - _sum01;
  end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 May 2019 04:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559843#M156398</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-18T04:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a dynamic logic based on data available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559845#M156399</link>
      <description>&lt;P&gt;What is it about your process that you don't know what the data structure is that you are receiving?&lt;/P&gt;
&lt;P&gt;Can't you just assume that the ratio variables will appear in the right order in the incoming dataset so that you can just use a variable list?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array ratio ratio_:;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why would they appear out of order?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 May 2019 04:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559845#M156399</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-18T04:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a dynamic logic based on data available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559851#M156403</link>
      <description>&lt;P&gt;Transpose the ratio_: variables to a long dataset, sort it, and apply your logic to the sequence. Then the code is completely data-driven.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See Maxim 33.&lt;/P&gt;</description>
      <pubDate>Sat, 18 May 2019 06:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-dynamic-logic-based-on-data-available/m-p/559851#M156403</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-18T06:35:46Z</dc:date>
    </item>
  </channel>
</rss>

