Something like this should work:
data have;
call streaminit(12345);
do date='01JAN2022'd to '31DEC2024'd by 7;
Amount =rand('integer', 100, 200);
output;
end;
run;
%macro summarize4(year, qtr);
proc sql;
select "&year" as Year
,"&qtr" as Quarter
,sum(Amount) as Total
,round(avg(Amount),.1) as Average
from have
where year(date)=&year
and qtr(date)=&qtr
;
quit;
%mend;
%summarize4(2023,2)
%summarize4(2024,1)
Result:
Year | Quarter | Total | Average |
---|---|---|---|
2023 | 2 | 2020 | 155.4 |
Year | Quarter | Total | Average |
---|---|---|---|
2024 | 1 | 1934 | 148.8 |
@Sas_rookie_sa wrote:
I have a data set with dates from all year long. I'm trying to set up a macro to pass a sysparm to only evaluate data with a given year and a given quarter
Are you going to do this repeatedly, such that all year/quarter combinations in your data set are "evaluated" (whatever that means)?
No need for a macro in this case. This will get the job done (it is calculating means, but other statistics are available):
proc summary data=have nway;
class date;
var amount;
format date yyq6.;
output out=want mean=;
run;
In fact, this seems like a more efficient approach than repeatedly calling a macro, it should execute much faster, especially on large data sets. Please explain how you would use such a macro.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.