<?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: Loop through tables at different periods and columns name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/941119#M369315</link>
    <description>&lt;P&gt;Sounds like you are trying to do feature generation to pass into your ML software?&lt;/P&gt;
&lt;P&gt;Not sure if ML software is smart enough to do the sort of temporal change calculations for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your described source data it would be trivial to combine the datasets using normal SAS code. MUCH MUCH easier than trying to do it with SQL code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have_p0 - have_p100;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To then generate your difference flags you could use ARRAYs in the data step.&lt;/P&gt;
&lt;P&gt;Say you have a series of 101 numeric variables named APPLE_P0 to APPLE_p100 and another series of character variables (let's call it NAME).&amp;nbsp; You could then generate a series of 100 difference variables comparing the value to the previous value (or perhaps to the period 0 value) using arrays and DO lops like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have_p0 - have_p100;
  by id;
  array _apple Apple_p0 - Apple_p100;
  array d_apple diff_apple_p1-diff_apple_p100;
  array _name Name_p0 - Name_p100;
  array d_name diff_name_p1-diff_name_p100;
  do index=2 to dim(_apple);
     d_apple[index-1] = _apple[index] - _apple[index-1] ;
     d_name[index-1] = _name[index]  ne _name[index-1] ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to extend that to a number of other variables then you could use code generation.&amp;nbsp; Either using macro language.&amp;nbsp; Or just using normal SAS code to write the generated SAS code to a file which can be included into the program using the %INCLUDE statement.&lt;/P&gt;</description>
    <pubDate>Tue, 27 Aug 2024 16:49:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-27T16:49:59Z</dc:date>
    <item>
      <title>Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940849#M369246</link>
      <description>&lt;DIV&gt;Hi everybody,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I would like to figure out the best way to solve this :&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;create a macro with a loop that substract (or if character indicate if difference) each variable from datasets of different periods (P0 - P1 until PX-1 - PX), and put the result in a new dataset. For example, apple_P0 - apple_P1, Orange_P0 - Orange_P1,then same for&amp;nbsp; Pineapple and TotalAB. For dataset P0 to dataset PX, increment by 1.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The dataset contains about 1000 colunms.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%let %n = 0;&lt;/DIV&gt;&lt;DIV&gt;%let %X = X; (max period)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data HAVE_P0;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; input ID Apple_P0 Orange_P0 Pineapple_P0 TotalAB_P0 $;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; cards;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; 15427 10&amp;nbsp; &amp;nbsp; 100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Machine&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; 35894 20&amp;nbsp; &amp;nbsp; 200&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Hand&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; 57842 40&amp;nbsp; &amp;nbsp; 400&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Hand&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; 79432 75&amp;nbsp; &amp;nbsp; 750&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7500&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Machine&lt;/DIV&gt;&lt;DIV&gt;;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data HAVE_P1;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; input ID Apple_P1 Orange_P1 Pineapple_P1 TotalAB_P1 $;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; cards;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; 15427&amp;nbsp; &amp;nbsp; &amp;nbsp;50 500 5000 Machine&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; 35894&amp;nbsp; &amp;nbsp; &amp;nbsp;10 100 1000 Machine&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; 57842&amp;nbsp; &amp;nbsp; &amp;nbsp;40 400 4000 Wind&lt;/DIV&gt;&lt;DIV&gt;;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data HAVE_P2;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; input ID Apple_P2 Orange_P2 Pineapple_P2 TotalAB_P2&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; cards;&lt;/DIV&gt;&lt;DIV&gt;...&lt;/DIV&gt;&lt;DIV&gt;data HAVE_PX;&lt;/DIV&gt;&lt;DIV&gt;etc.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;(&lt;/DIV&gt;&lt;DIV&gt;case if Var_P1 do not exist then .&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if Var_P0 is character and P0 = P1 then 1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if Var_P0 is character and P0 &amp;lt;&amp;gt; P1 then 0&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else Var_P0 - Var_P1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;end&lt;/DIV&gt;&lt;DIV&gt;)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data WANT_P0_minus_P1;&lt;/DIV&gt;&lt;DIV&gt;15427&amp;nbsp; &amp;nbsp; &amp;nbsp; 40 400 4000 1&lt;/DIV&gt;&lt;DIV&gt;35894 &amp;nbsp; -10 -100 -1000 0&lt;/DIV&gt;&lt;DIV&gt;57842&amp;nbsp; &amp;nbsp; &amp;nbsp; 0 0 0 0&lt;/DIV&gt;&lt;DIV&gt;79432&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;.&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data WANT_P1_minus_P2;&lt;/DIV&gt;&lt;DIV&gt;…&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data WANT_PX-1_minus_PX;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks in advance,&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 13:53:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940849#M369246</guid>
      <dc:creator>Rickie2</dc:creator>
      <dc:date>2024-08-26T13:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940852#M369247</link>
      <description>&lt;P&gt;What is the overall goal here?&amp;nbsp; What is the meaning of the data and what is the analysis you are trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does this P0, P1, etc suffix on the variable names mean?&lt;/P&gt;
&lt;P&gt;Why is the data in multiple datasets?&amp;nbsp; Why not just store all of the data in one dataset?&lt;/P&gt;
&lt;P&gt;Why do the variable names change between the datasets?&lt;/P&gt;
&lt;P&gt;Why not just have a separate variable with values like P0 or P1 (or perhaps numeric variable with 0 and 1) to indicate which P value this observation is for?&lt;/P&gt;
&lt;P&gt;Do you always have the same 100 variables?&lt;/P&gt;
&lt;P&gt;Do you have SAS/IML license?&amp;nbsp; Could you load the two datasets into matrices and just subtract them?&lt;/P&gt;
&lt;P&gt;Why do you need to make some many different difference datasets?&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 14:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940852#M369247</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-26T14:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940854#M369249</link>
      <description>&lt;P&gt;From your pseudo-code, why are variables that you say you want to 'subtract' apparently sometimes character???&lt;/P&gt;
&lt;P&gt;There are elements of the question that seem to indicate a poor data structure or management design at the base that is terribly complicating your data for what should be relatively simple actions.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 14:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940854#M369249</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-26T14:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940855#M369250</link>
      <description>&lt;P&gt;Why not just use PROC COMPARE?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE_P0;
    input ID Apple_P0 Orange_P0 Pineapple_P0 TotalAB_P0 $;
cards;
15427 10    100       1000         Machine
35894 20    200       2000         Hand
57842 40    400       4000         Hand
79432 75    750       7500         Machine
;
 
 
data HAVE_P1;
    input ID Apple_P1 Orange_P1 Pineapple_P1 TotalAB_P1 $;
cards;
15427     50 500 5000 Machine
35894     10 100 1000 Machine
57842     40 400 4000 Wind
;

proc compare data=have_p0 compare=have_p1 outall out=want noprint;
  id id;
  var Apple_P0 Orange_P0 Pineapple_P0 TotalAB_P0;
  with Apple_P1 Orange_P1 Pineapple_P1 TotalAB_P1;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need help getting the list of variable names perhaps you can let SAS calculate them for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select a.name 
       , b.name
    into :var0 separated by ' '
       , :var1 separated by ' '
  from
      (select name,type from dictionary.columns
        where libname='WORK' and memname='HAVE_P0'
          and upcase(name) like '%_P0'
       ) a
  inner join 
      (select name,type from dictionary.columns
        where libname='WORK' and memname='HAVE_P1'
          and upcase(name) like '%_P1'
       ) b
  on (upcase(substrn(a.name,1,length(a.name)-3))
   = upcase(substrn(b.name,1,length(b.name)-3)))
   and a.type=b.type
  order by 1
;
%let nvars=&amp;amp;sqlobs;
quit;

%put &amp;amp;=nvars;
%put &amp;amp;=var0;
%put &amp;amp;=var1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Aug 2024 15:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940855#M369250</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-26T15:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940990#M369296</link>
      <description>&lt;P&gt;Thanks for your answers Tom,&lt;/P&gt;&lt;P&gt;here are the answers for your questions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- What is the overall goal here?&amp;nbsp; What is the meaning of the data and what is the analysis you are trying to do?&lt;/P&gt;&lt;P&gt;Those data will be used for a ML model. A&amp;nbsp;left outer join brings all of tables together. The new table is going to be the dataset to modelized. Each table represent measures from different periods P: P0 current period, P1 the previous period, P2 the period before P1, ... until PX which is the ultimate period. That's why it required a loop. 1° P0-P1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2° P1-P2&amp;nbsp; &amp;nbsp; &amp;nbsp;3° P2-P3&amp;nbsp; &amp;nbsp; ....&amp;nbsp; &amp;nbsp; PX-1 - PX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- What does this P0, P1, etc suffix on the variable names mean?&amp;nbsp; Periods of time, example a year, measurement for a given year.&lt;/P&gt;&lt;P&gt;- Why is the data in multiple datasets?&amp;nbsp; Why not just store all of the data in one dataset?&amp;nbsp; &amp;nbsp;No problem, this step can be done before make a comparison step.&amp;nbsp; In this case, the variable names have to keep P0, P1, P2, etc. at the end of the variable name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Why do the variable names change between the datasets?&amp;nbsp; The value is computed period by period. Table Have_PO are results for the current year, Have_P1 results for the previous year, Have_P2 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why not just have a separate variable with values like P0 or P1 (or perhaps numeric variable with 0 and 1) to indicate which P value this observation is for?&amp;nbsp; &amp;nbsp; It's a possibility.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you always have the same 100 variables?&amp;nbsp; &amp;nbsp; Yes, periods P0, P1, P2,...,PX will always have the same variables&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have SAS/IML license?&amp;nbsp; Could you load the two datasets into matrices and just subtract them?&amp;nbsp; NO&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why do you need to make some many different difference datasets?&amp;nbsp; For a ML model, to follow the evolution between periods.&amp;nbsp; Then tables are united with a left outer join.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you have other questions, don't hesitate,&lt;/P&gt;&lt;P&gt;your help is greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 08:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/940990#M369296</guid>
      <dc:creator>Rickie2</dc:creator>
      <dc:date>2024-08-27T08:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through tables at different periods and columns name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/941119#M369315</link>
      <description>&lt;P&gt;Sounds like you are trying to do feature generation to pass into your ML software?&lt;/P&gt;
&lt;P&gt;Not sure if ML software is smart enough to do the sort of temporal change calculations for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your described source data it would be trivial to combine the datasets using normal SAS code. MUCH MUCH easier than trying to do it with SQL code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have_p0 - have_p100;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To then generate your difference flags you could use ARRAYs in the data step.&lt;/P&gt;
&lt;P&gt;Say you have a series of 101 numeric variables named APPLE_P0 to APPLE_p100 and another series of character variables (let's call it NAME).&amp;nbsp; You could then generate a series of 100 difference variables comparing the value to the previous value (or perhaps to the period 0 value) using arrays and DO lops like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have_p0 - have_p100;
  by id;
  array _apple Apple_p0 - Apple_p100;
  array d_apple diff_apple_p1-diff_apple_p100;
  array _name Name_p0 - Name_p100;
  array d_name diff_name_p1-diff_name_p100;
  do index=2 to dim(_apple);
     d_apple[index-1] = _apple[index] - _apple[index-1] ;
     d_name[index-1] = _name[index]  ne _name[index-1] ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to extend that to a number of other variables then you could use code generation.&amp;nbsp; Either using macro language.&amp;nbsp; Or just using normal SAS code to write the generated SAS code to a file which can be included into the program using the %INCLUDE statement.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 16:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-tables-at-different-periods-and-columns-name/m-p/941119#M369315</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-27T16:49:59Z</dc:date>
    </item>
  </channel>
</rss>

