DATA Step, Macro, Functions and more

arrays

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

arrays

[ Edited ]

I have  the following data

 


data test1;
	input sal hike;
	cards;
1000 2000
2000 3000
3000 4000
4000 5000
;
run;

data test2;
	set test1;
	array q1[1] sal;
	array q2[1] hike;
	array q3[1];

	do i=1 to 4;
		q3[i]=q1[i]-q2[i];
	end;
run;

 

 

 

I am gettng the following error:

 

216 data test1;
217 input sal hike;
218 cards;

NOTE: The data set WORK.TEST1 has 4 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


223 ;
224 run;
225 data test2;
226 set test1;
227 array q1[1] sal ;
228 array q2[1] hike;
229 array q3[1];
230 do i=1 to 4;
231 q3[i]=q1[i]-q2[i];
232 end;
233 run;

ERROR: Array subscript out of range at line 231 column 13.
sal=1000 hike=2000 q31=-1000 i=2 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.TEST1.
WARNING: The data set WORK.TEST2 may be incomplete. When this step was stopped there were 0
observations and 4 variables.
WARNING: Data set WORK.TEST2 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


Accepted Solutions
Solution
‎03-30-2017 04:24 PM
Super User
Posts: 10,549

Re: arrays

The proximate cause of the error you see is that your arrays Q1 and Q2 only have one element each.  So when i=2 in your loop there is no second, third or fourth element to address.

Arrays operate within rows of data not across observations which is what appears you want to do.

 

You may want:

data test2;
   set test1;
  q3 = sal - hire;
run;

if your goal is to get the difference of sal and hire for each pair. There would be a new variable to hold the value on each data row.

View solution in original post


All Replies
PROC Star
Posts: 7,366

Re: arrays

What are you trying to do? Specifically, given your example, what do you want the resulting file to look like?

 

Art, CEO, AnalystFinder.com

Solution
‎03-30-2017 04:24 PM
Super User
Posts: 10,549

Re: arrays

The proximate cause of the error you see is that your arrays Q1 and Q2 only have one element each.  So when i=2 in your loop there is no second, third or fourth element to address.

Arrays operate within rows of data not across observations which is what appears you want to do.

 

You may want:

data test2;
   set test1;
  q3 = sal - hire;
run;

if your goal is to get the difference of sal and hire for each pair. There would be a new variable to hold the value on each data row.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 146 views
  • 0 likes
  • 3 in conversation