0

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 :)

1
  • You can define the dataset to use to load a hash object when it is defined. Or are you really just asking for what options exist in SAS for converting a value into a decode? You could use a FORMAT or you could use the KEY= option on the SET statement. Commented Sep 7, 2015 at 22:07

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Great! It's exactly what I was looking for. Thank you very much ^-^
@Leo - You should consider adding some sample code to make this a self-contained answer.
@RobertPenridge, thanks for the advice. I concur. I've updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.