<?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: Filling in missing RECORDS in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9901#M2584</link>
    <description>A quick cludgy way, not elegant but it works and it should help get the concept:&lt;BR /&gt;
&lt;BR /&gt;
A is your Data Table&lt;BR /&gt;
&lt;BR /&gt;
/*Get the minimum Dates to find out where to start loops*/&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
CREATE TABLE A AS&lt;BR /&gt;
SELECT PERSON_ID, MIN(DATE) AS MIN_DATE&lt;BR /&gt;
FROM DATA_TAB&lt;BR /&gt;
GROUP BY PERSON_ID;&lt;BR /&gt;
&lt;BR /&gt;
/*Populate Dummy Date from Start Month to Current Month*/&lt;BR /&gt;
DATA BLANKER (KEEP=DATE I AMOUNT PERSON_ID);&lt;BR /&gt;
FORMAT MIN_DATE DATE9.;&lt;BR /&gt;
FORMAT DATE DATE9.;SET A;&lt;BR /&gt;
DIST=INTCK('MONTH',MIN_DATE,TODAY());&lt;BR /&gt;
DO I = 1 TO DIST-1;&lt;BR /&gt;
	DATE=INTNX('MONTH',MIN_DATE,I);&lt;BR /&gt;
	AMOUNT = 0;&lt;BR /&gt;
	OUTPUT;&lt;BR /&gt;
END;&lt;BR /&gt;
&lt;BR /&gt;
/*Sort the dummy data to get ready for merge*/&lt;BR /&gt;
PROC SORT DATA=BLANKER;&lt;BR /&gt;
BY PERSON_ID DATE;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* Interleave dummy date data and original data*/&lt;BR /&gt;
/* The I is to set up your counter to start from the start month*/&lt;BR /&gt;
DATA COMBINE;&lt;BR /&gt;
MERGE DATA_TAB BLANKER;&lt;BR /&gt;
BY PERSON_ID DATE;&lt;BR /&gt;
DATE_MT = MONTH(DATE);&lt;BR /&gt;
DATE_YR = YEAR(DATE);&lt;BR /&gt;
IF I EQ . THEN I = 1;&lt;BR /&gt;
ELSE I = I +1;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
/*Now Collpase the Dummy and Real Data*/&lt;BR /&gt;
/* FINAL is the table you want*/&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
FORMAT DATE DATE9.;&lt;BR /&gt;
CREATE TABLE FINAL AS&lt;BR /&gt;
SELECT PERSON_ID, MAX(DATE) AS DATE, MAX(AMOUNT) AS AMOUNT, MAX(I) AS MONTH&lt;BR /&gt;
FROM COMBINE&lt;BR /&gt;
GROUP BY PERSON_ID, DATE_MT, DATE_YR&lt;BR /&gt;
ORDER BY PERSON_ID, MONTH;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
Hope that helps get you started, I could do more elegant, but this works and helps describe what is going on.&lt;BR /&gt;
&lt;BR /&gt;
Ike Eisenhauer</description>
    <pubDate>Tue, 08 Apr 2008 23:08:36 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-04-08T23:08:36Z</dc:date>
    <item>
      <title>Filling in missing RECORDS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9900#M2583</link>
      <description>I have a dataset like this...&lt;BR /&gt;
&lt;BR /&gt;
person_id   date               amount&lt;BR /&gt;
102            25FEB2007     $25&lt;BR /&gt;
102            15MAY2007     $100&lt;BR /&gt;
102            11DEC2007     $30&lt;BR /&gt;
&lt;BR /&gt;
I'm trying to do the following...&lt;BR /&gt;
&lt;BR /&gt;
1. Create a new variable called "month" that represents the number of months starting from their first record. For example, the first record for person 102 would have month=1.&lt;BR /&gt;
&lt;BR /&gt;
2. But this person has no record for March or April so I need to add 2 records where month=2 and month=3, and the amount would be set to $0. I need to "fill in" records up to the current month.&lt;BR /&gt;
&lt;BR /&gt;
I'm guessing there is probably a "do" loop and "retain" involved and I'm hoping someone's got this wired already.</description>
      <pubDate>Tue, 08 Apr 2008 18:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9900#M2583</guid>
      <dc:creator>jawon</dc:creator>
      <dc:date>2008-04-08T18:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Filling in missing RECORDS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9901#M2584</link>
      <description>A quick cludgy way, not elegant but it works and it should help get the concept:&lt;BR /&gt;
&lt;BR /&gt;
A is your Data Table&lt;BR /&gt;
&lt;BR /&gt;
/*Get the minimum Dates to find out where to start loops*/&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
CREATE TABLE A AS&lt;BR /&gt;
SELECT PERSON_ID, MIN(DATE) AS MIN_DATE&lt;BR /&gt;
FROM DATA_TAB&lt;BR /&gt;
GROUP BY PERSON_ID;&lt;BR /&gt;
&lt;BR /&gt;
/*Populate Dummy Date from Start Month to Current Month*/&lt;BR /&gt;
DATA BLANKER (KEEP=DATE I AMOUNT PERSON_ID);&lt;BR /&gt;
FORMAT MIN_DATE DATE9.;&lt;BR /&gt;
FORMAT DATE DATE9.;SET A;&lt;BR /&gt;
DIST=INTCK('MONTH',MIN_DATE,TODAY());&lt;BR /&gt;
DO I = 1 TO DIST-1;&lt;BR /&gt;
	DATE=INTNX('MONTH',MIN_DATE,I);&lt;BR /&gt;
	AMOUNT = 0;&lt;BR /&gt;
	OUTPUT;&lt;BR /&gt;
END;&lt;BR /&gt;
&lt;BR /&gt;
/*Sort the dummy data to get ready for merge*/&lt;BR /&gt;
PROC SORT DATA=BLANKER;&lt;BR /&gt;
BY PERSON_ID DATE;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* Interleave dummy date data and original data*/&lt;BR /&gt;
/* The I is to set up your counter to start from the start month*/&lt;BR /&gt;
DATA COMBINE;&lt;BR /&gt;
MERGE DATA_TAB BLANKER;&lt;BR /&gt;
BY PERSON_ID DATE;&lt;BR /&gt;
DATE_MT = MONTH(DATE);&lt;BR /&gt;
DATE_YR = YEAR(DATE);&lt;BR /&gt;
IF I EQ . THEN I = 1;&lt;BR /&gt;
ELSE I = I +1;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
/*Now Collpase the Dummy and Real Data*/&lt;BR /&gt;
/* FINAL is the table you want*/&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
FORMAT DATE DATE9.;&lt;BR /&gt;
CREATE TABLE FINAL AS&lt;BR /&gt;
SELECT PERSON_ID, MAX(DATE) AS DATE, MAX(AMOUNT) AS AMOUNT, MAX(I) AS MONTH&lt;BR /&gt;
FROM COMBINE&lt;BR /&gt;
GROUP BY PERSON_ID, DATE_MT, DATE_YR&lt;BR /&gt;
ORDER BY PERSON_ID, MONTH;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
Hope that helps get you started, I could do more elegant, but this works and helps describe what is going on.&lt;BR /&gt;
&lt;BR /&gt;
Ike Eisenhauer</description>
      <pubDate>Tue, 08 Apr 2008 23:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9901#M2584</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-04-08T23:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Filling in missing RECORDS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9902#M2585</link>
      <description>[pre]&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=indata by person_id date;&lt;BR /&gt;
&lt;BR /&gt;
data expanded;&lt;BR /&gt;
  set indata;&lt;BR /&gt;
  cmonth = month(date);&lt;BR /&gt;
run;&lt;BR /&gt;
quit;&lt;BR /&gt;
 &lt;BR /&gt;
data expanded;&lt;BR /&gt;
  set expanded;&lt;BR /&gt;
  by person_id cmonth;&lt;BR /&gt;
  retain month sum;&lt;BR /&gt;
  &lt;BR /&gt;
  if first.person_id then month = 0;&lt;BR /&gt;
  if first.cmonth then do;&lt;BR /&gt;
    sum = 0;&lt;BR /&gt;
    month+1;&lt;BR /&gt;
  end;&lt;BR /&gt;
 &lt;BR /&gt;
  diff = cmonth - lag(cmonth);&lt;BR /&gt;
  &lt;BR /&gt;
  if diff &amp;lt; 2 then sum+amount;&lt;BR /&gt;
  else do i=1 to diff;&lt;BR /&gt;
    amount=0;&lt;BR /&gt;
    output;&lt;BR /&gt;
    month+1;&lt;BR /&gt;
  end;&lt;BR /&gt;
 &lt;BR /&gt;
  if last.cmonth then do;&lt;BR /&gt;
    amount = sum;&lt;BR /&gt;
    output;&lt;BR /&gt;
  end;&lt;BR /&gt;
 &lt;BR /&gt;
  drop cmonth sum diff i;&lt;BR /&gt;
end;&lt;BR /&gt;
 &lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 09 Apr 2008 12:31:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Filling-in-missing-RECORDS/m-p/9902#M2585</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-04-09T12:31:42Z</dc:date>
    </item>
  </channel>
</rss>

