BookmarkSubscribeRSS Feed
Sas_rookie_sa
Calcite | Level 5
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
2 REPLIES 2
SASJedi
Ammonite | Level 13

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

 

Check out my Jedi SAS Tricks for SAS Users
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 872 views
  • 2 likes
  • 3 in conversation