BookmarkSubscribeRSS Feed
JanKwiatkowski
Calcite | Level 5

Hello!
How to use hash object inside proc DS2, this code porduces Error:

proc ds2;
    data cars_test_two / overwrite=yes;
        /* Variable declarations */
        declare char(13) Make;
        declare char(8) Type; /* Adjusted length based on 'Sedan' */
        declare int _rc;

        /* Declare the hash object */
        declare package.hash codes();
        _rc = codes.defineKey('Make', 'Type');
        _rc = codes.defineDone();

        /* Load codes into hash object */
        method init(); 
            Make = 'Akura'; 
            Type = 'Sedan';
            _rc = codes.add();
            
            Make = 'Audi'; 
            Type = 'Sedan';
            _rc = codes.add();
        end;

        method run();
            set sashelp.cars (drop = DriveTrain);

            if codes.check() = 0 then
                output;
        end;
    enddata;
run;
quit;
ERROR: Compilation error.
ERROR: Parse encountered RC when expecting identifier.
ERROR: Line 74: Parse failed: declare int >>> _rc <<< ;
Please help to correct code.
1 REPLY 1
FreelanceReinh
Jade | Level 19

Hello @JanKwiatkowski,

 

I am a beginner when it comes to PROC DS2, but after fixing seven errors indicated in the logs (one per run) the code worked.

  1. PROC DS2 doesn't seem to like the variable name _rc. Changed it to rc (but _test works as well, so the underscore is not the issue).
  2. Replaced the period between "package" and "hash" with a blank.
  3. The DEFINEKEY and DEFINEDONE methods (unlike their siblings in the traditional DATA step) don't seem to create return codes.
  4. The DEFINEKEY method accepts only one argument at a time, so I called it twice. This limitation does not exist in the DATA step. Edit:  But there is a KEYS method, which does accept variable lists, of course using a different syntax than the DEFINEKEY method: codes.keys([Make Type]);
  5. Had to move "method init();" before the DEFINEKEY and DEFINEDONE method calls. So, this part of the program now looks like this:
    /* Declare the hash object */
    declare package hash codes();
    method init(); 
    codes.defineKey('Make');
    codes.defineKey('Type');
    codes.defineDone();
  6. Another DS2 vs. DATA step difference: The DROP= dataset option requires parentheses around the list of variable names (see below).
  7. PROC DS2 doesn't like concatenated libraries such as SASHELP. (Found this information in the 2016 thread Basic PROC DS2 Questions.) Simple libraries like my WORK library (or simple permanent libraries) can be used. So I created a work copy of SASHELP.CARS and used this: 
    set cars (drop = (DriveTrain));​
  8. Optional (just to avoid an unnecessary note in the log): Either declare variable rc as double (because int "is not supported by the BASE driver") or, better, drop it from the output dataset:
    data cars_test_two (drop = (rc)) / overwrite=yes;​

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 454 views
  • 2 likes
  • 2 in conversation