Hi,
I'm writing a program that stores lookup data in a multi-dimensional array and I need to return the row index of the array to eliminate hard coding of values. The sas functions whichc and whichn are designed for this but they return the position of the lookup value expressed as a list.
data want;
array foo [3,5] $2 _temporary_
(
'a1' 'b1' 'c1' 'd1'
'a2' 'b2' 'c2' 'd2'
'a3' 'b3' 'c3' 'd3'
);
drop i;
do i = 1 to dim(foo);
key = foo[i,1];
does_lkup_as_1_dimension = whichc(key,of foo[*]);
real_index = int(whichc(key,of foo[*])/dim2(foo))+1;
output;
end;
run;
The above code returns the following values for a lookup on a 3 * 4 dimension array in the variable 'does_lkup_as_1_dimension'.
key | does_lkup_as_1_dimension | real_index |
a1 | 1 | 1 |
a2 | 6 | 2 |
a3 | 11 | 3 |
I am using this as a workaround which returns the value I want.
real_index = int(whichc(key,of foo[*])/dim2(foo))+1;
Is there a better way with native SAS functions? I'm surprised I have not needed to do this until now and I expect I'm not the first!
Thanks in advance.
Cam
data want;
array foo [3,5] $2 _temporary_
(
'a1' 'b1' 'c1' 'd1'
'a2' 'b2' 'c2' 'd2'
'a3' 'b3' 'c3' 'd3'
); drop i;
do i = 1 to dim(foo);
key = foo[i,1];
do j=1 to dim2(foo);
if foo[i,j]=key then do;
does_lkup_as_1_dimension=i ;
output;
end;
end;
end;
run;
proc print;run;
Thanks @Ksharp I'm merely looking to return the index of the array in the most efficient way possible so I can do additional lookups on the array. I've used whichc as it's essentially a one liner vs. nested loop.
For context the real array in my code contains a key value, a regex pattern and then a series of flags that is being used to tokenize the contents of a file (A SAS Log). An array is used vs. hash table because the order of the regex patterns is important. My code applies a token to each line of the file (first dimension of the array). I then need to re-use the array to apply a series of business rules which the flags in the array are used for. Thanks for responding though.
Is there a reason you define the array as 3*5, but provide values implying 3*4?
I think what you want is the CEILing function applied to the whichc value divided by the row length (dim(foo,2)):
data want;
array foo [3,4] $2 _temporary_
(
'a1' 'b1' 'c1' 'd1'
'a2' 'b2' 'c2' 'd2'
'a3' 'b3' 'c3' 'd3'
);
do key='a1','a2','b3';
row_index=ceil(whichc(key,of foo{*})/dim(foo,2));
col_index=mod(whichc(key,of foo{*})-1,dim(foo,2))+1;
output;
end;
run;
proc print;run;
No need to add 1 for row index.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.