Hi all,
I'd like to create a table where the count of something is displayed by day. I am interested in also displaying the days where the count was zero. Here's what I have, which obviously only yields dates where count for that day was >=1.
PROC SQL ;
CREATE TABLE graph_data as
SELECT
dtevent,
count(*) as totalnum
FROM latestcounts
GROUP BY dtevent
ORDER By dtevent ;
QUIT ;
proc sql noprint;
select min(dtevent) , max(dtevent) into :sdate,:edate from latestcounts;
quit;
data dateev;
do dtevent=&sdate to &edate;
output;
end;
run;
PROC SQL ;
CREATE TABLE graph_data as
SELECT
l.dtevent,case when r.totalnum=. then 0 else r.totalnum end as totalnum
from dateev as L left join
(SELECT
dtevent, count(*) as totalnum
FROM latestcounts
GROUP BY dtevent) as R
on L.dtevent = R.dtevent
;
QUIT ;
an other solution If you don t have proc timeseries
1- attribute min and max dtevent into 2 macro variable
2- create a serie of date between min & max date
3- left join with proc sql
Unfortunately proc timeseries writes warnings to the log - which is a great idea in other scenarios, but because we want the gaps to be filled i would not expect warnings telling me that the procedure did what i wanted it to do. After a quick glance in the docs i was not able to find an option to disable the warnings, but perhaps i have overlooked something.
proc sql noprint;
select min(dtevent) , max(dtevent) into :sdate,:edate from latestcounts;
quit;
data dateev;
do dtevent=&sdate to &edate;
output;
end;
run;
PROC SQL ;
CREATE TABLE graph_data as
SELECT
l.dtevent,case when r.totalnum=. then 0 else r.totalnum end as totalnum
from dateev as L left join
(SELECT
dtevent, count(*) as totalnum
FROM latestcounts
GROUP BY dtevent) as R
on L.dtevent = R.dtevent
;
QUIT ;
an other solution If you don t have proc timeseries
1- attribute min and max dtevent into 2 macro variable
2- create a serie of date between min & max date
3- left join with proc sql
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.