BookmarkSubscribeRSS Feed
PeterClemmensen
Tourmaline | Level 20

Hi.

 

Can anyone tell me why I can't reference some entry in an array with the observation pointer _N_?

 

lets sat that I have some dataset and wanted to reference the entry in the array corresponding to the current observation number, what approach should I use?

 

data have;
input CharVar $ Numvar;
datalines;
A 1
B 2
C 3
;
run;

data ArrayTest;
   set have;
   
   Array SomeArray{*} Numvar;
   
   NewVar = SomeArray[_N_];
run;

The above gives me an error.

7 REPLIES 7
data_null__
Jade | Level 19

Your ARRAY does not have enough elements. 

PeterClemmensen
Tourmaline | Level 20

Doesn't it have three elements? One for each row in the NumVar column?

Shmuel
Garnet | Level 18

You probably want to convert your input data into an array.

you can do it using PROC TRANSPOSE  or 

by a datastep like:

 

data have;
   retain var1 - var3;
   array varx var1-var3 ;    /* replace 3 in var3 at least to the expected number of rows or more */
input CharVar $ Numvar;
   varx(_N_) = Numvar;
    if charvar = 'Z' then output;     /* I added that row to assign end of input */

datalines;

A 1

B 2

C 3
Z .
;

run;

/* your HAVE dataset shall have just onw row with n=max(_N_) ovariables: var1 to varn */

data_null__
Jade | Level 19

You are confusing observations(records) and variables.  What are you trying to do?

Shmuel
Garnet | Level 18
what did you meant by
Array SomeArray{*} Numvar;
ballardw
Super User

Your array has exaclyt one element: Numvar. It only has the value on a current row as the SAS data step processes one row of data at a time. Let's repeat that: SAS data step only sees one row at a time. You have to do extra (some times a lot extra) work to reference values in a different row of the data and sometimes not very practical in a data step at all.

 

_N_ being in effect the row number as soon as you get to the second row the value is 2 and exceeds the size of the array and generates errors.

 

If you want to address ROW and COLUMN at the same time for all of the data then you need to move over to PROC IML to deal with matrices.

PeterClemmensen
Tourmaline | Level 20

Thank you all 🙂 This makes sense.

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
  • 7 replies
  • 868 views
  • 3 likes
  • 4 in conversation