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
SAS Super FREQ

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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