<?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: PROC SQL Do loop with case when in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835795#M330453</link>
    <description>I want to update a table with a variable coming from another table.  Problem is that this variable can come from many datasets that have the same structure but each of these datasets are constructed for a specific date (e.g dataset name : BASE_201801 for jan,2018) that contain 2 variables : ID and quantity (the variable that i want to add). So to add this variable i have to match the ID but also the date of the observation in my main dataset with the date in the dataset name that have the variable that i want to add. So how can i specify that table A's date variable have to be equal to the date in the  other dataset name ?</description>
    <pubDate>Thu, 29 Sep 2022 09:37:39 GMT</pubDate>
    <dc:creator>elsfy</dc:creator>
    <dc:date>2022-09-29T09:37:39Z</dc:date>
    <item>
      <title>PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835782#M330444</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can i loop this ?&lt;/P&gt;
&lt;PRE&gt;proc sql ; 

create table want as 
select a.* , 
CASE WHEN a.date = b.date then b.RD
     WHEN a.date = c.date then c.RD
     ELSE 0 
END AS RD

from list a 
left join base_201801 b on a.ID= b.iD /* Jan,2018 */
left join base_201802 c on a.ID = c.ID /* Feb, 2018*/
/* and so on with bases until 202209 corresponding to Sept, 2022*/
;
quit ; &lt;/PRE&gt;
&lt;P&gt;This code is working but i want to add many more conditions to the CASE WHEN statement like if the date in b is not found in a then look into table c, else look in table d, etc&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 08:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835782#M330444</guid>
      <dc:creator>elsfy</dc:creator>
      <dc:date>2022-09-29T08:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835793#M330452</link>
      <description>&lt;P&gt;Sure, from a syntax perspective you can add more tables and extend your CASE statement.&lt;/P&gt;
&lt;P&gt;You encapsulate it into a macro to make it more dynamic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But looking at your code and data structure, I wondering what you actually want to achieve?&lt;/P&gt;
&lt;P&gt;If you have tables segmented by month (but with the same structure), joins are usally not the most efficent way of working.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 09:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835793#M330452</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2022-09-29T09:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835795#M330453</link>
      <description>I want to update a table with a variable coming from another table.  Problem is that this variable can come from many datasets that have the same structure but each of these datasets are constructed for a specific date (e.g dataset name : BASE_201801 for jan,2018) that contain 2 variables : ID and quantity (the variable that i want to add). So to add this variable i have to match the ID but also the date of the observation in my main dataset with the date in the dataset name that have the variable that i want to add. So how can i specify that table A's date variable have to be equal to the date in the  other dataset name ?</description>
      <pubDate>Thu, 29 Sep 2022 09:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835795#M330453</guid>
      <dc:creator>elsfy</dc:creator>
      <dc:date>2022-09-29T09:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835796#M330454</link>
      <description>If possible, i don't want to merge all datasets that have the variable that i want to add to my main dataset</description>
      <pubDate>Thu, 29 Sep 2022 09:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835796#M330454</guid>
      <dc:creator>elsfy</dc:creator>
      <dc:date>2022-09-29T09:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835857#M330478</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/419704"&gt;@elsfy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;If possible, i don't want to merge all datasets that have the variable that i want to add to my main dataset&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This join is likely to take much more time to accomplish that APPENDING (merge is side-by-side, append is vertical stacking) your data sets together, not to mention that you have to account for changes in the code when the next monthly data set gets created.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 14:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835857#M330478</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-29T14:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Do loop with case when</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835875#M330488</link>
      <description>&lt;P&gt;You have a data design that makes it difficult to look up the desired information.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As posted in your previous question here, the best solution is to combine the tables and then merge the data in.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/ADD-value-from-other-tables/m-p/835691#M330407" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/ADD-value-from-other-tables/m-p/835691&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want an optimal methodology it would be to first look at all the months in the dataset and then create a view of those tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then merge with the appended version.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doing a join where you're searching more tables will be more inefficient than appending and is more cumbersome to code so not sure how this is a better solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will a record be in more than the month indicated?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 15:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Do-loop-with-case-when/m-p/835875#M330488</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-09-29T15:33:49Z</dc:date>
    </item>
  </channel>
</rss>

