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.
Your ARRAY does not have enough elements.
Doesn't it have three elements? One for each row in the NumVar column?
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 */
You are confusing observations(records) and variables. What are you trying to do?
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.
Thank you all 🙂 This makes sense.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.