BookmarkSubscribeRSS Feed
fred_major
Calcite | Level 5

Hello I need a little help with missing Dates.

My dataset contains a list of about 100 products with total sales by day.
Say for example the date range starts July 1st and ends July 18th.

Some products have dates & totals missing for July 1st, July 10th and July 18th.

How can I populate a new dataset that contains everything from the original dataset but with any missing dates for each product with total being 0?

Thanks

Fred

4 REPLIES 4
Reeza
Super User

Create a data set that has the list of dates you need and then left join on the data sets.

ie

data all_dates;

start_date='01Jul2011'd;

end_date = '18Jul2011'd;

num_days=end_date-start_date+1;

     do i=1 to num_days;

          date=start_date+1;

          total=0;

     end;

run;

Then join the dataset to yours and verify the values are set to 0.

proc sql;

create table test1 as

select a.*, b.*
from all_dates a

left join have b

on a.date=b.date;

quit;

Another option is the lag function...check if the previous date-current-date is 1 but then you could have consecutive dates missing and things can get complex. You'll probably have to tweak the above code to get the date ranges correctly set and the joins to get the data you need.

I find this a simple and straightforward solution.

art297
Opal | Level 21

Fred,

Here is a rather brute force solution:

data have;

  informat date date9.;

  input product date total;

  cards;

1 01jul2011 1

1 03jul2011 5

1 17jul2011 2

2 02jul2011 3

2 18jul2011 4

3 02jul2011 3

3 17jul2011 4

;

proc sql noprint;

  create table products as

    select distinct product

      from have

;

data products;

  set products;

  do date='01jul2011'd to '18jul2011'd;

    output;

  end;

run;

data want;

  merge have products;

  by product date;

  total=max(total,0);

run;

HTH,

Art

Ksharp
Super User

Or

data have;

  informat date date9.;
  input product date total;
  cards;
1 01jul2011 1
1 03jul2011 5
1 17jul2011 2
2 02jul2011 3
2 18jul2011 4
3 02jul2011 3
3 17jul2011 4
;

 

proc sql noprint;
  create table products as
    select distinct product
      from have
; quit;

 

data products;
  set products;
  do date='01jul2011'd to '18jul2011'd;
    output;
  end;
run;

 

data want;
  merge have products;
  by product date;
run;
proc stdize data=want out=w missing=0 reponly;
 var total;
run;

Ksharp

fred_major
Calcite | Level 5

Thanks everyone, I always learn something new when I post in this forum.

Just what I needed.

Fred

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1281 views
  • 0 likes
  • 4 in conversation