BookmarkSubscribeRSS Feed
foobarbaz
Obsidian | Level 7

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'.

keydoes_lkup_as_1_dimensionreal_index
a111
a262
a3113

 

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

Regards,
Cameron | Selerity
6 REPLIES 6
Ksharp
Super User
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;
foobarbaz
Obsidian | Level 7

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.

 

  

Regards,
Cameron | Selerity
mkeintz
PROC Star

Is there a reason you define the array as 3*5, but provide values implying 3*4?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
foobarbaz
Obsidian | Level 7

Hi,

No it's just a typo on my part for the example.

Regards,
Cameron | Selerity
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
foobarbaz
Obsidian | Level 7

Hi @mkeintz ,

That makes a lot more sense.  The int(...) + 1 part felt like a hack to me.

 

 

Regards,
Cameron | Selerity

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1238 views
  • 1 like
  • 3 in conversation