Help using Base SAS procedures

Array subscript out of range at...

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Array subscript out of range at...

[ Edited ]

Hi Team,

 

I have read a lot of posts on that Error, but unfortunately none of these could solve my Error.

So I have a dataset with the variables d_2006, erw_2006 for each year till d_2015 and erw_2015 and only one observation.

I want to compute the SMR for all of these years within a macro.

The variables smr, smr_u, and smr_o don't exist but should be created by the array.

 

data out_&number;							
	set out;
		amph = "&nenner"; 					

		array smr [10] smr2006 smr2007 smr2008 smr2009 smr2010 smr2011 smr2012 smr2013 smr2014 smr2015;
		array smr_u [10] smr_u2006 smr_u2007 smr_u2008 smr_u2009 smr_u2010 smr_u2011 smr_u2012 smr_u2013 smr_u2014 smr_u2015;
		array smr_o [10] smr_o2006 smr_o2007 smr_o2008 smr_o2009 smr_o2010 smr_o2011 smr_o2012 smr_o2013 smr_o2014 smr_o2015;
		array erw_ erw_2006-erw_2015;
		array d_ d_2006-d_2015;
		do i = 2006 to 2015;

		if erw_{i} =0 then smr{i} = d_{i};
		if erw_{i} >0 then smr{i} = d_{i}/erw_{i};

		if d_{i} =0 then smr_u{i} =0; 
		if d_{i} >0 & erw_{i} >0 then smr_u{i} = (d_{i}*((1-(1/(9*d_{i}))-(1.959963985/(3*sqrt(d_{i})))))**3)/erw_{i}; 
		if d_{i} >0 & erw_{i} =0 then smr_u{i} = (d_{i}*((1-(1/(9*d_{i}))-(1.959963985/(3*sqrt(d_{i})))))**3);	

		if erw_{i} >0 then smr_o{i} = ((d_{i}+1)*((1-(1/(9*(d_{i}+1)))+(1.959963985/(3*(sqrt(d_{i}+1))))))**3)/erw_{i};
		if erw_{i} =0 then smr_o{i} = ((d_{i}+1)*((1-(1/(9*(d_{i}+1)))+(1.959963985/(3*(sqrt(d_{i}+1))))))**3);
		end;
		drop i; 
run;

I've tried a lot of changes but stil get the ERROR: Array subscript out of range at...

 

Does anybody know where the fault is?

 

That would be great! Thanks a lot for help.

 

 


Accepted Solutions
Solution
Thursday
Super User
Posts: 9,865

Re: Array subscript out of range at...

Two solutions.

1)


		do i = 1 to 10;

 2)

array smr [2006:2015] smr2006 smr2007 smr2008 smr2009 smr2010 smr2011 smr2012 smr2013 smr2014 smr2015;
		array smr_u [2006:2015] smr_u2006 smr_u2007 smr_u2008 smr_u2009 smr_u2010 smr_u2011 smr_u2012 smr_u2013 smr_u2014 smr_u2015;
		array smr_o [2006:2015] smr_o2006 smr_o2007 smr_o2008 smr_o2009 smr_o2010 smr_o2011 smr_o2012 smr_o2013 smr_o2014 smr_o2015;
		array erw_[2006:2015] erw_2006-erw_2015;
		array d_[2006:2015] d_2006-d_2015;
		do i = 2006 to 2015;

View solution in original post


All Replies
Solution
Thursday
Super User
Posts: 9,865

Re: Array subscript out of range at...

Two solutions.

1)


		do i = 1 to 10;

 2)

array smr [2006:2015] smr2006 smr2007 smr2008 smr2009 smr2010 smr2011 smr2012 smr2013 smr2014 smr2015;
		array smr_u [2006:2015] smr_u2006 smr_u2007 smr_u2008 smr_u2009 smr_u2010 smr_u2011 smr_u2012 smr_u2013 smr_u2014 smr_u2015;
		array smr_o [2006:2015] smr_o2006 smr_o2007 smr_o2008 smr_o2009 smr_o2010 smr_o2011 smr_o2012 smr_o2013 smr_o2014 smr_o2015;
		array erw_[2006:2015] erw_2006-erw_2015;
		array d_[2006:2015] d_2006-d_2015;
		do i = 2006 to 2015;
Super User
Posts: 7,405

Re: Array subscript out of range at...

All your arrays have members from 1 to 10, but you let your index variable count from 2006 to 2015. Won't do.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 11

Re: Array subscript out of range at...

Great, thank you! That was easy! Smiley Happy

Highlighted
Super User
Super User
Posts: 7,695

Re: Array subscript out of range at...

Classic Excel thinking!  You could save yourself some headaches and re-model your data:

YEAR   SMR    SMR_U    SMR_O  ERW  D

 

In this way your data goes down the page, and calculations and such like are simplified down to their base coding, for example:

if d_{i} >0 & erw_{i} >0 then smr_u{i} = (d_{i}*((1-(1/(9*d_{i}))-(1.959963985/(3*sqrt(d_{i})))))**3)/erw_{i}; 

To:

if d > 0 and erw > 0 then smr_u=(d*((1-(1/(9*d))-(1.959963985/(3*sqrt(d)))))**3)/erw; 

Another benefit to this setup is that it does not matter how many years there are, if the number expands to 50 or 100 years the code remains untouched, in your example however the code expands rapidly.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 114 views
  • 4 likes
  • 4 in conversation