BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
novinosrin
Tourmaline | Level 20

I would like to use obj.remove or obj.removedup at my own will and need based on a condition or just at a time when hiter points to the hash table. Soon as dcl hiter points to hash table and at the time of iteration backwards or forwards, it makes sense to lock the hash table as the hiter points to the hash table. Any option to override this behavior?

 

Thank you and happy long weekend

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@novinosrin

You can't delete items from a hash if a hiter object is linked to this hash.

You can mimic what a hiter can do for you by defining a hash with a constant key and then use the do_over() hash method to iterate over the whole hash.

data test(drop=_:);

  if _n_=1 then
    do;
      /* map columns for hash */
      if 0 then 
        do;
          set sashelp.class(keep=name sex rename=(name=h_name sex=h_sex));
          length _key $1;
          retain _key '1';
        end;
      /* define the hash */
      dcl hash h1(multidata:'y');
      h1.defineKey('_key');
      h1.defineData('h_name', 'h_sex');
      h1.defineDone();
      /* load data into the hash */
      do _i=1 to nobs;
        set sashelp.class(keep=name sex rename=(name=h_name sex=h_sex)) nobs=nobs point=_i;
        h1.add();
      end;
    end;

  set sashelp.class;

  do while(h1.do_over() eq 0);
    if name=h_name then
      do;
        h1.removedup();
        leave;
      end;
  end;

run;

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

I don't understand.  Could you provide a stepwise sequence to illustrate what you want?

 

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

--------------------------
Patrick
Opal | Level 21

@novinosrin

You can't delete items from a hash if a hiter object is linked to this hash.

You can mimic what a hiter can do for you by defining a hash with a constant key and then use the do_over() hash method to iterate over the whole hash.

data test(drop=_:);

  if _n_=1 then
    do;
      /* map columns for hash */
      if 0 then 
        do;
          set sashelp.class(keep=name sex rename=(name=h_name sex=h_sex));
          length _key $1;
          retain _key '1';
        end;
      /* define the hash */
      dcl hash h1(multidata:'y');
      h1.defineKey('_key');
      h1.defineData('h_name', 'h_sex');
      h1.defineDone();
      /* load data into the hash */
      do _i=1 to nobs;
        set sashelp.class(keep=name sex rename=(name=h_name sex=h_sex)) nobs=nobs point=_i;
        h1.add();
      end;
    end;

  set sashelp.class;

  do while(h1.do_over() eq 0);
    if name=h_name then
      do;
        h1.removedup();
        leave;
      end;
  end;

run;
novinosrin
Tourmaline | Level 20

Thank you sir @Patrick. Thats' exactly what i wanted to learn and know. Any "intuition" to why the lock can't be unlocked aka remove items from an original design structure point of view? opinion?

That piece will give me some smile to have comprehended after all.

I would appreciate your one more final response with a reference to hiter functionality in this case and sincerely close the thread.

Many thanks indeed. 

Patrick
Opal | Level 21

@novinosrin

I don't know how the hiter has been implemented and though can only guess that it establishes some sort of pointers to the underlying hash which would cause an error condition if removing the hash item to which a pointer points to.  ....but that's really just me making up some explanation.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 472 views
  • 2 likes
  • 3 in conversation