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

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 6 replies
  • 882 views
  • 5 likes
  • 5 in conversation