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

Hi

I have a dataset with two years of data, broken down by minute. Most minutes are accounted for, but some minutes have no data and are missing. I want to fill in these missing minutes and assign them a value of 0. Is there a simple way to do this, perhaps using proc expand?

For example, if I have

MinuteValule
12:141
12:157
12:189
12:194

I would like to expand this to:

MinuteValue
12:141
12:157
12:160
12:170
12:189
12:194

Thanks for any suggestions.

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

DATA have;

informat minute time.;

format minute time.;

infile cards dsd;

input Minute value;

cards;

12:14,1

12:15,7

12:18,9

12:19,4

;

run;

proc expand data=have out=want from=minute to=minute observed=end method=none;

    id minute;

run;

View solution in original post

5 REPLIES 5
Steelers_In_DC
Barite | Level 11

DATA have;

informat minute time.;

format minute time.;

infile cards dsd;

input Minute value;

cards;

12:14,1

12:15,7

12:18,9

12:19,4

;

run;

proc expand data=have out=want from=minute to=minute observed=end method=none;

    id minute;

run;

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

With no SAS/ETS, a DATA step approach works where DO/END with OUTPUT loop creates a "seed" file with the VALUE retained as 0.

Then merge (with a suitable BY statement) the _FILL seed file onto the source data to create a merged copy - the MERGE statement file order matters here.

Scott Barry

SBBWorks, Inc.

bsam17
Calcite | Level 5

Thanks, Mark! This seems to work almost perfectly, but I have two questions:

1) I get warnings in the log whenever it fills in a minute. How do I supress this warning?

2) How would I carry down the last value instead of filling them in as missings?

Steelers_In_DC
Barite | Level 11

You want to use a by statement to fill in the blanks:

proc expand data=have out=want from=minute to=minute observed=end method=none;

     by id;

    id minute;

run;

You can write out the log to anywhere you want but I never recommend doing this.  It would go like this:

proc printto log="/sas/user_data/local/filename.log"; run;

proc expand data=have out=want from=minute to=minute observed=end method=none;

     by id;

    id minute;

run;

proc printto;run;

Anytime I've used this the log was unusable, I would advise you to find out why you are getting the warnings and try to make an adjustment.  If the warnings make the other part of the log unusable then I might do this but I only use it as a last resort.

Ksharp
Super User

Code: Program

DATA have;
informat minute time.;
format minute time.;
infile cards dsd;
input Minute value;
cards;
12:14,1
12:15,7
12:18,9
12:19,4
;
run;

data want;
merge have have(firstobs=2 keep=minute rename=(minute =_m));
output;
do i=minute+60 to ifn(missing(_m),0,_m-60) by 60;
  minute=i;
  value=0;
  output;
end;
drop i _m;
run;

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!

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