<?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: Creating tables with proc sql based on subqueries? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243275#M268466</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me try again. &amp;nbsp;Post some example test data - in the form of a datastep, and what you want the output to look like. &amp;nbsp;Each of the replies given are of the same form - create the elements you need in the final dataset, then merge this back to original data. &amp;nbsp;The merge takes care of missing records. &amp;nbsp;However we can't provide exact scenarios without seeing what you have and want.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jan 2016 17:55:29 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-01-13T17:55:29Z</dc:date>
    <item>
      <title>Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243218#M268445</link>
      <description>&lt;P&gt;Hi, I am trying to create a new table or data set differently based on logic that looks for specific records in a data set.&amp;nbsp; For example, I want to create a new data set with a WTD, MTD, YTD, and LTD column from the original data set if all of those exist.&amp;nbsp; If they don't (in my case there is no WTD in the original data set) then I want to insert a dummy WTD record and union it to the MTD, YTD, and LTD records from the original data set for the new data set.&amp;nbsp; The way I have written the code, I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: A Composite expression (usually a subquery) is used incorrectly in an expression.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I have written.&amp;nbsp; Is there a way to acomplish what I am tring to do?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT_APP as
		SELECT DISTINCT
			CASE 
				WHEN EXISTS
					(
					  select * 
					  from FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT a3
					  where TIMEFRAME = 'WTD'
					)
				THEN 
					(
						select 
					   		b.TIMEFRAME,
							b._TYPE_ as _TYPE_,
							b._PAGE_ as _PAGE_,
							b._TABLE_ as _TABLE_,
							b.INVESTOR_LOAN_ID_N,
							b.NOTE_BALANCE_Sum,
							b.NOTE_BALANCE_Mean,
							b.WGT_AVG_20YR_Mean,
							b.LTV_Mean,
							b.FICO_Mean,
							b.SRP_RATE_Mean,
							b.SRP_AMT_Mean,
							b.SRP_AMT_Sum,
							b.PCT_TOTAL_UPB_PURCHASE_Mean,
							b.PCT_TOTAL_UPB_REFI_Mean,
							b.PCT_TOTAL_CNT_ESCROW_Mean,
							b.PCT_TOTAL_CNT_NONESCROW_Mean,
							CASE
								WHEN b.TIMEFRAME = 'WTD' then 1
								WHEN b.TIMEFRAME = 'MTD' then 2
								WHEN b.TIMEFRAME = 'YTD' then 3
								WHEN b.TIMEFRAME = 'LTD' then 4
							END as TIMEFRAME_NO
						from FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT b
						/*order by TIMEFRAME_NO*/
					)
				else 
					(
						SELECT 
							'WTD' as TIMEFRAME,
							'1' as _TYPE_,
							1 as _PAGE_,
							1 as _TABLE_,
							0 as INVESTOR_LOAN_ID_N,
							0 as NOTE_BALANCE_Sum,
							0 as NOTE_BALANCE_Mean,
							0 as WGT_AVG_20YR_Mean,
							0 as LTV_Mean,
							0 as FICO_Mean,
							0 as SRP_RATE_Mean,
							0 as SRP_AMT_Mean,
							0 as SRP_AMT_Sum,
							0 as PCT_TOTAL_UPB_PURCHASE_Mean,
							0 as PCT_TOTAL_UPB_REFI_Mean,
							0 as PCT_TOTAL_CNT_ESCROW_Mean,
							0 as PCT_TOTAL_CNT_NONESCROW_Mean,
							1 as TIMEFRAME_NO
					    from FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT a	
					    union 
					    select 
					   		b.TIMEFRAME,
							b._TYPE_ as _TYPE_,
							b._PAGE_ as _PAGE_,
							b._TABLE_ as _TABLE_,
							b.INVESTOR_LOAN_ID_N,
							b.NOTE_BALANCE_Sum,
							b.NOTE_BALANCE_Mean,
							b.WGT_AVG_20YR_Mean,
							b.LTV_Mean,
							b.FICO_Mean,
							b.SRP_RATE_Mean,
							b.SRP_AMT_Mean,
							b.SRP_AMT_Sum,
							b.PCT_TOTAL_UPB_PURCHASE_Mean,
							b.PCT_TOTAL_UPB_REFI_Mean,
							b.PCT_TOTAL_CNT_ESCROW_Mean,
							b.PCT_TOTAL_CNT_NONESCROW_Mean,
							CASE
								WHEN b.TIMEFRAME = 'WTD' then 1
								WHEN b.TIMEFRAME = 'MTD' then 2
								WHEN b.TIMEFRAME = 'YTD' then 3
								WHEN b.TIMEFRAME = 'LTD' then 4
							END as TIMEFRAME_NO
						from FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT b
						/*order by TIMEFRAME_NO*/
					) END as x
		FROM FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT a;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243218#M268445</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T15:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243220#M268446</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, thats a bit TL;DR for me. &amp;nbsp;I can tell at a brief glance that that wont work or wont be the best way of doing it. &amp;nbsp;I would recommend posting a small datastep with some test data in, just a few lines, and what you want the output to look like. &amp;nbsp;At a guess on the logic, first create a distinct table of the data you want out, then just left join onto that the data from your main table where the condition is true.&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  distinct ...
  from    HAVE A
  left join (select ... where condition is true) B
  on ...;
quit;&lt;/PRE&gt;
&lt;P&gt;Its vague as I have no test data.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243220#M268446</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-13T15:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243224#M268447</link>
      <description>&lt;P&gt;Thanks RW9.&amp;nbsp; I was not sure if a left join would work since this would be acting like a merge or union.&amp;nbsp; Maybe if I lay it out like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Original Table has the following observations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;&lt;P&gt;YTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;&lt;P&gt;LTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In that table, there is no WTD.&amp;nbsp; In the new table I need a WTD so I would create a "dummy" record with zeros for the other fields.&amp;nbsp; I need to check to see if that WTD exists initially to determine if I need a dummy record or not.&amp;nbsp; If it exists, then I simply need to take all data from the original table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Final table would look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WTD, field 2, field 3, etc. (whether this is from dummy data or the data in the original table)&lt;/P&gt;&lt;P&gt;MTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;&lt;P&gt;YTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;&lt;P&gt;LTD&amp;nbsp; field 2, field 3, etc.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243224#M268447</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T15:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243227#M268448</link>
      <description>&lt;P&gt;Missing level problem. Create a table that has all the levels you need. &amp;nbsp;Then you merge them together and if a value doesn't exist the join will add it in, but if all records are present it will work as intended.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243227#M268448</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-13T15:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243228#M268449</link>
      <description>&lt;P&gt;Data is key here to understanding, post some test data. &amp;nbsp;Here is a an example (verbose coding but I am just leaving for the day). &amp;nbsp;Create a template dataset, then merge back to it:&lt;/P&gt;
&lt;PRE&gt;data have;
  id=1; a="MTD"; field2=1; output;
  id=1; a="YTD"; field2=1; output;
  id=1; a="LTD"; field2=1; output;
  id=2; a="MTD"; field2=1; output;
  id=2; a="YTD"; field2=1; output;
  id=2; a="LTD"; field2=1; output;
  id=2; a="WTD"; field2=1; output;
run;

proc sort data=have out=tmp nodupkey;
  by id;
run;
data tmp;
  set tmp (keep=id);
  do a="MTD","YTD","LTD","WTD";
    output;
  end;
run;

proc sql;
  create table WANT as
  select  A.*,
          B.FIELD2
  from    TMP A
  left join HAVE B
  on      A.ID=B.ID
  and     A.A=B.A;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243228#M268449</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-13T15:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243230#M268450</link>
      <description>&lt;P&gt;Reeza, can you point me to an example of this?&amp;nbsp; I am new to this one.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:46:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243230#M268450</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T15:46:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243231#M268451</link>
      <description>&lt;P&gt;Thanks, I will give this a try too.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243231#M268451</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T15:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243232#M268452</link>
      <description>&lt;P&gt;This sounds like a good example of when to use macro language.&amp;nbsp; To prepare, get two working versions of&amp;nbsp; your program.&amp;nbsp; One should work when WTD does exist, and the other should work when WTD doesn't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When those are ready, it is likely a straightforward macro language application.&amp;nbsp; Determine whether WTD exists, and have the macro implement one version of the program or the other.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once we're that far, we can talk about the best implementation that allows for sets of variables to exist or not.&amp;nbsp; (You won't need dozens of versions of a working program.&amp;nbsp; Macro language will take care of that.)&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243232#M268452</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-13T15:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243233#M268453</link>
      <description>&lt;P&gt;It&amp;nbsp;looks like&amp;nbsp;you try to use a CASE structure to create a new &lt;EM&gt;table&lt;/EM&gt; conditionally. However, to my knowledge, you can only use it to create a new &lt;EM&gt;variable&lt;/EM&gt;. Also, in the beginning you talk about a WTD &lt;EM&gt;column&lt;/EM&gt;, but later about a WTD &lt;EM&gt;record&lt;/EM&gt;&amp;nbsp;(i.e row) and in your code 'WTD' occurs only as a value of a character variable TIMEFRAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your second post you show a dataset with only three observations. Is this true also for your real data or do you have more records there, maybe with a key variable (not shown so far; see RW9's variable ID) and blocks of 3&amp;nbsp;or 4 records with TIMEFRAME values 'MTD', 'YTD', 'LTD' and possibly&amp;nbsp;&lt;SPAN style="line-height: 20px;"&gt;'WTD'? If this is the case, I think you could achieve what you want even without merging, PROC SQL, auxiliary&amp;nbsp;datasets or macros, in a single data step.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:51:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243233#M268453</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-13T15:51:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243234#M268454</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;solution is the method I was suggesting.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, you require a master list. Then merge your data into the master table, and any missing records are added in via the join.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243234#M268454</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-13T15:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243236#M268455</link>
      <description>&lt;P&gt;Sorry for the confusion about this.&amp;nbsp; Yes, WTD, MTD, YTD, and LTD would be values of the TIMEFRAME variable in the data set.&amp;nbsp; The data set has several variables other than TIMEFRAME for each observation.&amp;nbsp; My goal is to create a dummy observation for WTD if it doesn't exist in the original data set, otherwise, just pull the observations for WTD, MTD, YTD, and LTD that will be in the original data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that is a better explanation.&amp;nbsp; Again, sorry for the confusion.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 15:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243236#M268455</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T15:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243241#M268456</link>
      <description>&lt;P&gt;I have taken this code and modified it for my data.&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 FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT;
	id=1; a="MTD"; _TYPE_=1; output;
	id=1; a="YTD"; _TYPE_=1; output;
	id=1; a="LTD"; _TYPE_=1; output;
	id=2; a="WTD"; _TYPE_=1; output;
	id=2; a="MTD"; _TYPE_=1; output;
	id=2; a="YTD"; _TYPE_=1; output;
	id=2; a="LTD"; _TYPE_=1; output;
run ;

proc sort data=flowfred.fmflow_loansfunded_20yr_rpt out=tmp nodupkey;
	by id;
run;

data tmp;
	set tmp (keep=id _TYPE_);
	do a="WTD","MTD","YTD","LTD";
		output;
	end;
run;

proc sql;
	create table FLOWFRED.FMFLOW_LOANSFUNDED_20YR_RPT_APP as
	select A.*,
		b._TYPE_
	from tmp A
	left join flowfred.fmflow_loansfunded_20yr_rpt b on a.id=b.id
		and a.a=b.a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here are the results I am getting from this.&amp;nbsp; Note that currently the original data set currently has an observation that includes WTD in the variable TIMEFRAME.&amp;nbsp; How should I interpret the results I am getting?&amp;nbsp; Is this what would be expected?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp; _TYPE_&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WTD&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; MTD&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; YTD&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; LTD&lt;BR /&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WTD&lt;BR /&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MTD&lt;BR /&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; YTD&lt;BR /&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LTD&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243241#M268456</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T16:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243242#M268457</link>
      <description>&lt;P&gt;So, assuming your existing dataset (call it HAVE) has only 3 or 4 observations (depending on whether an observation with TIMEFRAME='WTD' exists), you could create the new dataset (call it WANT) as shown below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Scenario 1: WTD record exists */

data have;
input timeframe $ field2 field3;
cards;
MTD 1 11
YTD 2 22
LTD 3 33
WTD 4 44
;

data want;
set have end=last;
wtdex+(timeframe='WTD');
output;
if last &amp;amp; not wtdex then do;
  call missing(of _all_);
  timeframe='WTD';
  output;
end;
drop wtdex;
run;

/* Scenario 2: WTD record does not exist */

data have;
set have(obs=3);
run;

/* data want; ... same solution as above */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:18:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243242#M268457</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-13T16:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243246#M268458</link>
      <description>&lt;P&gt;Does that solution extend to past WTD? What if LTD is missing instead?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243246#M268458</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-13T16:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243249#M268459</link>
      <description>&lt;P&gt;If you do have a key variable such as ID and blocks of 3 or 4 observations (the number possibly varying from one ID to the next), you can still follow the simple one-data-step approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id timeframe $ field2 field3;
cards;
1 MTD 11 101
1 YTD 21 201
1 LTD 31 301
2 MTD 12 102
2 YTD 22 202
2 LTD 32 302
2 WTD 42 402
;

data want;
set have;
by id;
if first.id then wtdex=0;
wtdex+(timeframe='WTD');
output;
if last.id &amp;amp; not wtdex then do;
  call missing(of _all_);
  timeframe='WTD';
  output;
end;
drop wtdex;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 17:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243249#M268459</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-13T17:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243250#M268460</link>
      <description>&lt;P&gt;Because I am not familiar with the cards; piece of this... I am having a little trouble understanding.&amp;nbsp; Maybe if I add the remaining variables for the data set (have and want) then you can help me translate that a little bit.&amp;nbsp; Here are all the variables...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIMEFRAME&lt;/P&gt;&lt;P&gt;_TYPE_&lt;BR /&gt;_PAGE_&lt;BR /&gt;_TABLE_&lt;BR /&gt;INVESTOR_LOAN_ID_N&lt;BR /&gt;NOTE_BALANCE_Sum&lt;BR /&gt;NOTE_BALANCE_Mean&lt;BR /&gt;WGT_AVG_20YR_Mean&lt;BR /&gt;LTV_Mean&lt;BR /&gt;FICO_Mean&lt;BR /&gt;SRP_RATE_Mean&lt;BR /&gt;SRP_AMT_Mean&lt;BR /&gt;SRP_AMT_Sum&lt;BR /&gt;PCT_TOTAL_UPB_PURCHASE_Mean&lt;BR /&gt;PCT_TOTAL_UPB_REFI_Mean&lt;BR /&gt;PCT_TOTAL_CNT_ESCROW_Mean&lt;BR /&gt;PCT_TOTAL_CNT_NONESCROW_Mean&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243250#M268460</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T16:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243252#M268461</link>
      <description>&lt;P&gt;Good point,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;. I&amp;nbsp;assumed (based on elwayfan446's posts) that only 'WTD' could be missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24237"&gt;@elwayfan446﻿&lt;/a&gt;: Would the requested code need to fill in missing 'MTD', 'YTD' or 'LTD' records as well? What if all four were missing? How would we&amp;nbsp;&lt;SPAN style="line-height: 20px;"&gt;know the ID (if there is one)?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;I think, the number and names of additional variables are less important. We could name them field2, field3 etc. for the time being without loss of generality.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;The only important question is: Do we have an "ID" (and blocks of 3 to 4 or maybe 1&amp;nbsp;to 4 observations per ID) or are we talking about a HAVE dataset with max. 4 observations?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:48:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243252#M268461</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-13T16:48:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243255#M268462</link>
      <description>&lt;P&gt;Cards is used to generate sample data to test code. Providing a variable list does not help, providing sample data that mimics your issue is helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can read here on how to produce a&amp;nbsp;Minimum, Complete and Verifable example.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://stackoverflow.com/help/mcve" target="_blank"&gt;http://stackoverflow.com/help/mcve&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243255#M268462</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-13T16:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243256#M268463</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt; While unlikely, I suppose there could be a missing MTD, YTD, and LTD.&amp;nbsp; However, if WTD is not missing then it spills over into the others.&amp;nbsp; That being said, having code to check that in case might not be a bad idea.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't have an ID specific to the observations other than the TIMEFRAME variable.&amp;nbsp; The HAVE dataset will only have a max of 4 observations (WTD, MTD, YTD, and LTD)&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 16:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243256#M268463</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T16:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating tables with proc sql based on subqueries?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243259#M268464</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp; Thank you for this reference.&amp;nbsp; This is great to know going forward.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 17:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-tables-with-proc-sql-based-on-subqueries/m-p/243259#M268464</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-01-13T17:02:52Z</dc:date>
    </item>
  </channel>
</rss>

