BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
avinashns
Obsidian | Level 7

I have a data set  with respect to dates as below.

I would like to identify and insert the missing date series in each group and rest of the variables value should be previous record data.

 

Data have:

Date                 Employee    var2    var3    var4 ..... ...

01aug2021      Tom

02aug2021      Tom

04aug2021      Tom

05aug2021      Tom

06aug2021      Tom

01Sep2021      John

03sep2021      John

01Sep2021      John

 

As you can see 03aug2021 is missing in under TOM and 02Sep2021 is missing from John group.

Data Want:

Date                 Employee    var2    var3    var4 ..... ...

01aug2021      Tom

02aug2021      Tom

03aug2021      Tom

04aug2021      Tom

05aug2021      Tom

06aug2021      Tom

01Sep2021      John

02Sep2021      John

03sep2021      John

01Sep2021      John

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input Date     : date9.            Employee  $;
format date date9.;
cards;
01aug2021      Tom
02aug2021      Tom
04aug2021      Tom
05aug2021      Tom
06aug2021      Tom
01Sep2021      John
03sep2021      John
04Sep2021      John
;

data want;
 merge have have(firstobs=2 keep=date employee rename=(date=_date employee=_employee));
 output;
 if employee=_employee then do;
  do date=date+1 to _date-1;
   output;
  end;
 end;
drop _:;
run;

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ

Hello,

 

I have only a minute but, if not solved by tomorrow, I can elaborate on this topic.

 

For the missing dates, my colleague Gerhard Svolba has published numerous community library articles on this topic, using the SAS/ETS procedures PROC TIMESERIES or PROC EXPAND or PROC TIMEDATA and maybe also PROC TIMEID (??). See the Communities Library.

 

The missing value imputation method you are looking after is called "Last Value Carry Forward" (LVCF) or "forward-filling". Search these communities with those keywords and you will find numerous hits (numerous topics on this subject).

 

Good luck,

Koen

sbxkoenk
SAS Super FREQ

On top of my previous reply (see above) ...

 

Replace MISSING VALUES in TIMESERIES DATA using PROC EXPAND and PROC TIMESERIES
Posted 02-04-2021 10:45 AM | by gsvolba
https://communities.sas.com/t5/SAS-Communities-Library/Replace-MISSING-VALUES-in-TIMESERIES-DATA-usi...

 

Using the TIMESERIES procedure to check the continuity of your timeseries data
Posted 01-28-2021 03:40 PM | by gsvolba
https://communities.sas.com/t5/SAS-Communities-Library/Using-the-TIMESERIES-procedure-to-check-the-c...

 

Koen

 

avinashns
Obsidian | Level 7
Hi,
Even if we are able get what are the missing dates between the max and min dates of each employee that will also be fine later we can fill other values. like
03aug2021 Tom * Employee Tom missing one date
02Sep2021 John * employee John missing one date
05Sep2021 Dave * employee Dave missing two date
08Sep2021 Dave * employee Dave missing two date ... etc
Thanks in advance
mkeintz
PROC Star

You basically want to carry forward a given record over any internal "holes" in the series:

data want (drop=nxt_:);
  set have (keep=employee);
  by employee;
  merge have  have (firstobs=2 keep=date rename=(date=nxt_date));
  output;
  if last.employee=0 then do while (date<nxt_date-1);
      date=date+1;
    output; 
  end;
run;

The first SET statement is there just to generate the first.employee and last.employee dummies for testing in an IF statement.  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input Date     : date9.            Employee  $;
format date date9.;
cards;
01aug2021      Tom
02aug2021      Tom
04aug2021      Tom
05aug2021      Tom
06aug2021      Tom
01Sep2021      John
03sep2021      John
04Sep2021      John
;

data want;
 merge have have(firstobs=2 keep=date employee rename=(date=_date employee=_employee));
 output;
 if employee=_employee then do;
  do date=date+1 to _date-1;
   output;
  end;
 end;
drop _:;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2026 views
  • 1 like
  • 4 in conversation