Is there any easy way to create a hash object in SAS from a hash-like data set (i.e. a data set where one column is treated like Key and another -- like Value)?
Thanks and sorry if it's an easy question, I'm novice in SAS :)
Is there any easy way to create a hash object in SAS from a hash-like data set (i.e. a data set where one column is treated like Key and another -- like Value)?
Thanks and sorry if it's an easy question, I'm novice in SAS :)
I think you are looking for the dataset: argument tag. I lifted an example from the SAS doc and simplified it to be minimal:
data table;
input key data $;
datalines;
531 yellow
620 green
908 orange
143 purple
;
data _null_;
length key 8 data $ 8;
if (_n_ = 1) then do;
declare hash myhash(dataset: "table");
rc = myhash.definekey('key');
rc = myhash.definedata('data');
myhash.defineDone();
call missing( key, data ); /* avoid note re uninitialized values */
end;
rc = myhash.find(key:908);
put data=;
run;
Your log should show:
NOTE: There were 4 observations read from the data set WORK.TABLE.
DATA=orange
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
If you have a SAS/OR license you could also use OPTMODEL:
proc optmodel;
set KEY;
str color{KEY};
read data table into KEY=[key] color=data;
put color[908]=;
quit;