<?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: SQL Case statement fill in blanks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667349#M199816</link>
    <description>&lt;P&gt;There lookup tables for the most part.&amp;nbsp; I have one fact table and one dim.&amp;nbsp; Creating the other dims requires top level sign off at the moment.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jul 2020 03:41:07 GMT</pubDate>
    <dc:creator>DavidPhillips2</dc:creator>
    <dc:date>2020-07-07T03:41:07Z</dc:date>
    <item>
      <title>SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667274#M199773</link>
      <description>&lt;P&gt;I'm thinking proc SQL is the best way to go about this.&amp;nbsp; I have a fact table that has Id and time.&amp;nbsp; The corresponding tables that work with the fact table have less time entries than the fact table.&amp;nbsp; I need to join the other tables with the fact tables on Id and time then fill in blanks that come in from the other tables based on the most recent date up to that point in time.&amp;nbsp; My time dimension is a character string formatted like 201010 202110 202210 formatted as year-academic period.&amp;nbsp; The reason I want to use proc SQL and case statements is because I have about 10 lookup tables like the table below and its easy to use proc SQL with lookup tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise on how to fill in the blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test tables are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	create table fact_example (
	academic_period varchar2(6),
	id int,
	retained int);

	insert into fact_example (academic_period, id, retained)
	values ('201010', 111, 1);
	insert into fact_example (academic_period, id, retained)
	values ('201110', 111, 1);
	insert into fact_example (academic_period, id, retained)
	values ('201210', 111, 1);
	insert into fact_example (academic_period, id, retained)
	values ('201310', 111, 1);

	create table dim_example (
	academic_period varchar2(6),
	id int,
	variable_changes varchar(1));

	insert into dim_example (academic_period, id, variable_changes)
	values ('201010', 111, 'b');

	insert into dim_example (academic_period, id, variable_changes)
	values ('201210', 111, 'g');


	Create table lookup_example (
	varshort varchar(1),
	varlong varchar(50));

	insert into lookup_example (varshort, varlong) values ('b', 'blue');
	insert into lookup_example (varshort, varlong) values ('g', 'green');
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Jul 2020 19:10:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667274#M199773</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-07-06T19:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667289#M199777</link>
      <description>&lt;P&gt;So, in your example, the combination of the three tables would give:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;academic  id  retained    varlong
_period
201010    111               1    blue
201110    111               1    
201210    111               1    green
201310    111               1    &lt;/PRE&gt;
&lt;P&gt;and what would the filled-in table contain?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 20:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667289#M199777</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-06T20:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667290#M199778</link>
      <description>&lt;PRE&gt;academic  id 	retained  varlong
_period
201010   111 1 blue&lt;BR /&gt;201110 	 111 1 blue&lt;BR /&gt;201210   111 1 green
201310 	 111 1 green    	 

&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Jul 2020 20:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667290#M199778</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-07-06T20:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667291#M199779</link>
      <description>&lt;P&gt;And you need to do this exclusively with SQL? Do you eventually need to move this SQL code back to some dbms?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 20:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667291#M199779</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-06T20:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667296#M199782</link>
      <description>&lt;P&gt;For that code block doing it in proc SQL would likely save a lot of code, given 10+ lookup tables.&amp;nbsp; After I have that block of data queried I'm switching back and forth between proc SQL and datasteps for the rest of the ETL.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 20:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667296#M199782</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-07-06T20:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667300#M199783</link>
      <description>&lt;P&gt;Do you have a separate dim table for every lookup table? What is the purpose of the dim table?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 21:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667300#M199783</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-06T21:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667340#M199810</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;For that code block doing it in proc SQL would likely save a lot of code, given 10+ lookup tables.&amp;nbsp; After I have that block of data queried I'm switching back and forth between proc SQL and datasteps for the rest of the ETL.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Fact tables are normally high volume, dimension and reference tables often low volume. If using a SQL then you'll end up with a lot of sorting.&lt;/P&gt;
&lt;P&gt;Consider using a data step with hash table lookups.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 01:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667340#M199810</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-07T01:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667348#M199815</link>
      <description>&lt;P&gt;Actually, it might be best to do step one as a data step selecting the data and then do the lookups in proc sql.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 03:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667348#M199815</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-07-07T03:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667349#M199816</link>
      <description>&lt;P&gt;There lookup tables for the most part.&amp;nbsp; I have one fact table and one dim.&amp;nbsp; Creating the other dims requires top level sign off at the moment.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 03:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667349#M199816</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-07-07T03:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667354#M199819</link>
      <description>&lt;P&gt;Code like below would work for your sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

  set fact_example;
  by id academic_period;

  if _n_=1 then 
    do;
      if 0 then set dim_example;
      dcl hash h1(dataset:'dim_example');
      h1.defineKey('academic_period', 'id');
      h1.defineData('variable_changes');
      h1.defineDone();
      drop variable_changes;

      if 0 then set lookup_example;
      dcl hash h2(dataset:'lookup_example');
      h2.defineKey('varshort');
      h2.defineData('varlong');
      h2.defineDone();
      drop varshort;
    end;

  if h1.find()=0 then h2.find(key:variable_changes);
  output;

  if last.id then call missing(of _all_);
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1594099034036.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46959iC35D49F4C7C78FBF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1594099034036.png" alt="Patrick_0-1594099034036.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 05:19:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667354#M199819</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-07T05:19:20Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667408#M199851</link>
      <description>&lt;P&gt;Why are you using SQL for the simple creation of tables? This is THE domain of data steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fact_example;
input academic_period :$6. id retained;
datalines;
201010 111 1
201110 111 1
201210 111 1
201310 111 1
;

data dim_example;
input academic_period :$6. id variable_changes :$1.;
datalines;
201010 111 b
201210 111 g
;

data lookup_example;
input varshort :$1. varlong :$50.;
datalines;
b blue
g green
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 11:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667408#M199851</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-07T11:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Case statement fill in blanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667412#M199854</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;For that code block doing it in proc SQL would likely save a lot of code, given 10+ lookup tables.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doing that in SQL is akin to shooting yourself in the foot with a double-barreled magnum shotgun. It looks simple (one SQL step), but will in fact cause several sorts of the fact table.&lt;/P&gt;
&lt;P&gt;Unless your fact table is so small that the whole operation is not time-consuming at all.&lt;/P&gt;
&lt;P&gt;Doing that with 10 hashes in a single data step (that reads the large table sequentially just once) is the way to go, unless the lookups are too large to fit in memory all at once. Then you may have to split the operation into several steps.&lt;/P&gt;
&lt;P&gt;(Using the examples from my previous post)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set fact_example;
if _N_ = 1
then do;
  length variable_changes $1;
  declare hash dim (dataset:'dim_example');
  dim.definekey('academic_period','id');
  dim.definedata('variable_changes');
  dim.definedone();
  length varlong $50;
  declare hash lookup (dataset:'lookup_example (rename=(varshort=variable_changes))');
  lookup.definekey('variable_changes');
  lookup.definedata('varlong');
  lookup.definedone();
  call missing(variable_changes,varlong);
end;
retain variable_changes;
rc = dim.find();
rc = lookup.find();
drop rc variable_changes;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Jul 2020 11:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Case-statement-fill-in-blanks/m-p/667412#M199854</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-07T11:41:14Z</dc:date>
    </item>
  </channel>
</rss>

