BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
molla
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 641 views
  • 0 likes
  • 3 in conversation