Hello all,
I am at my witts end trying to figure this out. I have a dataset that Has ID Hour Date. If the data I receive does not have each hour (0-23) I need to add it for the ID and Date. If the Date is Missing I also need to add the Date along with the Hour. The Date is on a 7 day block and I run this report once a week.
Here is the code I have to this point:
proc sort data = want;
by ID Hours DATE;
run;
proc means data = want noprint;
by ID Hours DATE;
var Minutes_Since_Previous_Scan;
output out= Look;
run;
proc sort data = Look;
by ID Hours Date;
run;
proc transpose data=Look out=Transpose;
by ID Hours Date;
id _STAT_;
var Minutes_Since_Previous_Scan;
run;
proc sort data = transpose;
by Date Hours;
run;
proc sort data = Insert;
by Date Hours;
run;
data merger;
merge insert transpose ;
by Date Hours;
run;
Problem with this code is it will only insert the hour and date onto the first IDI have to this point. I need it to continue to look at all IDs in the set and continue to add the hour for each date if it is not present. I have made a sheet that would read it but to update the IDs every week only hundreds of IDs is too cumbersome. Any help is appreciated. Thanks,
An EXCEL file is not much value, but if you convert it to an actual dataset with DAY and HOUR variables then you can make the skeleton dataset very easily with an SQL cross join query.
So if the set of ID values is in HAVE and the set of DAY*HOUR combinations is in DAYS then the to make the ALL dataset just use:
proc sql;
create table all as
select distinct a.id,b.day,b.hour
from have a
, days b
order by 1,2,3
;
quit;
To fill in the missing day*hour just take the distinct values of ID and make a skeleton dataset with just those three variables.
Do you know the 7 days?
Do you want 24 hours per day? From 0 to 23?
data all;
set have (keep=id);
by id;
if first.id;
do date=&date1 to &date2 ;
do hour=0 to 23 ;
output;
end;
end;
run;
data want;
merge all have ;
by id date hour ;
run;
What values do you want for the OTHER variables on those new observations?
This example will leave them as MISSING.
Thank you for the reply. It is the 7 days of the week based on the report that I am working for. I created an excel file with the dates and the Hours for each date but I do not have the by variable of ID. Is the code you sent still applicable of does it need adjustment with this new information?
An EXCEL file is not much value, but if you convert it to an actual dataset with DAY and HOUR variables then you can make the skeleton dataset very easily with an SQL cross join query.
So if the set of ID values is in HAVE and the set of DAY*HOUR combinations is in DAYS then the to make the ALL dataset just use:
proc sql;
create table all as
select distinct a.id,b.day,b.hour
from have a
, days b
order by 1,2,3
;
quit;
I could kiss you TOM!! Thanks a ton this is exactly what I needed.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.