BookmarkSubscribeRSS Feed
postollma
Fluorite | Level 6

I would like to basically use the following code 

%let year = measles1900 - measles2001;

versus listing out each year

%let year = measles1900 measles1901 measles1902 measles1903 measles1904 measles1905 measles1906 measles1907 measles1908 measles1909 ...

and so on.

 

Do I need to use a command along the lines of %nstr, etc., am I able to use an array somehow, or am I out of luck and need to resort to listing all 102 years?

6 REPLIES 6
Reeza
Super User
It depends entirely on how you're planning to use this macro variable later on.
postollma
Fluorite | Level 6

@Reeza I have measles(YEAR) as different datasets.

After that let statement, I want to do a proc summary as follows:

proc summary data=&year;
var countvalue;
output out=total_&year sum=;
run;

so that I can get column totals for the variable countvalue by year.

Reeza
Super User
Then neither will work. Your code has to be valid SAS syntax and I don't believe either of those are valid SAS syntax.

You need to either combine the data sets in a view ahead of time, or loop through and do it for each data set. I recommend create the view approach first.
ChrisHemedinger
Community Manager

Maybe a CALL EXECUTE structure?

 

data range;
 do yr=1900 to 2001 by 1;
  year = put(yr,4.);
  call execute
  (
    "
    proc summary data=measles" || year || ";
    var countvalue;
    output out=total_" || year || " sum=;
    run;
    "
  );
 end;
run;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
s_lassen
Meteorite | Level 14

I assume that you want to

  1. collect data from all the years in one dataset
  2. get the year as a variable

 

So you will have to collect all the data first. 

%let indata=measles1900-measles2001;

data v_measles/view=v_measles;
  set  &indata indsname=table;
  year=input(substr(table,length(table)-3),4.0);
run;
  

I did the data collection as a view, as there is no reason to duplicate the actual data. You can now do your summary on the data step view:

proc summary data=v_measles nway;
  var countvalue;
  class year;
  output out=total sum=;
run;
Satish_Parida
Lapis Lazuli | Level 10

I am assuming your proc summery is working, please find the code.

 

%macro runit;
	%let years = measles1900 measles1901 measles1902 measles1903 measles1904 measles1905 measles1906 measles1907 measles1908 measles1909;

	%let y_count=%sysfunc(countw(&years, ' '));

	%do i=1 %to &y_count.;
		%let year=%sysfunc(scan(&years,&i));
		%put &=year;
		
		proc summary data=&year;
		var countvalue;
		output out=total_&year sum=;
		run;
	%end;
	
%mend runit;
%runit;

Please let us know if it worked for you.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 2265 views
  • 5 likes
  • 5 in conversation