libname abc 'C:\'; %let gpath='C:\'; %let dpi=200; ods html close; ods listing gpath=&gpath image_dpi=&dpi; /*--Sort by onset date and virus--*/ proc sort data=abc.Test_dataset out=dataset; by donset virus; run; /*--get onset date range and generate YearWeek variable--*/ data dataset2; retain min 1e10 max -1e10; set dataset(keep=year donset virus week) end=last; if virus='V2' then virus='A1'; if virus='W1' then virus='B2'; if virus='W3' then virus='C3'; min=min(min, donset); max=max(max, donset); yearWeek=year || '-' || put(week, z2.0); if last then do; call symput ("Min", min); call symput ("Max", max); end; run; /*--Compute frequency values by virus, year, week--*/ proc means data=dataset2; class yearweek virus; var week; output out=freq(where=(_type_ > 2)) N=N; run; /*--Sort by YearWeek and virus--*/ proc sort data=freq(keep=yearWeek virus n) out=sorted; by yearweek virus; run; /*--Create an observation for each week in date range--*/ data fill; drop prev; prev=-1; virus='A1'; do donset=&min to &max; year=year(donset); week=week(donset)+1; yearWeek=year || '-' || put(week, z2.0); if week ne prev then do; output; prev=week; end; end; run; /*--Merge data and add data for labels--*/ data merged; merge fill sorted; by yearWeek virus; zero=0; if virus='A1' then small=-0.1; else small=0; run; /*--This creates an dattrmap dataset to set the correct color values for the curves --*/ data attrmap; length ID $ 9 fillcolor $ 20; input id $ value $ fillcolor $; datalines; virus A1 darkgreen virus B2 orange virus C3 darkblue ; run; /*--Virus counts by YearWeek--*/ ods graphics on / reset height=3 in width=6in imagename='Virus_BarChart93'; proc sgplot data= merged dattrmap=attrmap; vbar yearWeek / response=n group=virus attrid=virus nooutline; xaxis label='Onset Week' valueattrs=(size=6) display=none; run; /*--Virus counts by YearWeek with week label--*/ ods graphics on / reset height=3 in width=6in imagename='Virus_BarChartLabel93'; title 'Onset by Week and Virus'; proc sgplot data= merged dattrmap=attrmap; vbar yearWeek / response=n group=virus attrid=virus nooutline; vbar yearWeek / response=zero group=virus nooutline datalabel=week datalabelattrs=(size=5) fillattrs=(color=white); xaxis label='Onset Week' valueattrs=(size=6) display=none; yaxis values=(0 to 10 by 2) valueshint label='Frequency'; run; /*--Virus counts by YearWeek with week label--*/ ods graphics on / reset height=3 in width=6in imagename='Virus_BarChartLabelBelow93'; title 'Onset by Week and Virus'; proc sgplot data= merged dattrmap=attrmap; vbar yearWeek / response=n group=virus attrid=virus nooutline; vbar yearWeek / response=small group=virus nooutline datalabel=week datalabelattrs=(size=5) fillattrs=(color=white); refline 0 / lineattrs=(thickness=1 color=black); xaxis label='Onset Week' valueattrs=(size=6) display=none; yaxis values=(0 to 10 by 2) valueshint label='Frequency' ; run;