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

Dear all,

 

Is any way to add key that not in dataset to hash? Just like:

dcl hash h(dataset:"sashelp.class”);
h.definekey("key","sex");
h.definekey(all:"Y");
h.definedone();

I try to use _N_ ,but failed.

dcl hash h(dataset:"sashelp.class”);
h.definekey("_N_","sex");
h.definekey(all:"Y");
h.definedone();

Or I try to use in option, failed as well.

dcl hash h(dataset:"sashelp.class(in=key)”);
h.definekey("key","sex");
h.definekey(all:"Y");
h.definedone();

So is there any possible to add a key that does not exist in the dataset when I use the dataset option in hash?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Once you issue

 

dcl hash h(dataset:"sashelp.class”);

you can't specify any variables in the definekey or definedata method that are not in sashelp.class.

 

 

To do what you apparently want to do, you can either:

 

data vt /view=vt;
  set sashelp.class;
  _n=_n_;
run;

data _null_;
  if 0 then set vt;
  if _n_=1 then do;
    declare hash h (dataset:'vt');
      h.definekey('sex','_n');
	  h.definedata(all:'Y');
	  h.definedone();
  end;
  *....  other code ...;
run;

or

 

data _null_;
  set sashelp.class end=end_of_class;
  _n=_n_;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('sex','_n');
      h.definedata('name','sex','age','height','weight','_n');
      h.definedone();
  end;
  h.add();
  if end_of_class;
  *..... other code ...;
run;

 

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

--------------------------

View solution in original post

8 REPLIES 8
mkeintz
PROC Star

Once you issue

 

dcl hash h(dataset:"sashelp.class”);

you can't specify any variables in the definekey or definedata method that are not in sashelp.class.

 

 

To do what you apparently want to do, you can either:

 

data vt /view=vt;
  set sashelp.class;
  _n=_n_;
run;

data _null_;
  if 0 then set vt;
  if _n_=1 then do;
    declare hash h (dataset:'vt');
      h.definekey('sex','_n');
	  h.definedata(all:'Y');
	  h.definedone();
  end;
  *....  other code ...;
run;

or

 

data _null_;
  set sashelp.class end=end_of_class;
  _n=_n_;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('sex','_n');
      h.definedata('name','sex','age','height','weight','_n');
      h.definedone();
  end;
  h.add();
  if end_of_class;
  *..... other code ...;
run;

 

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

--------------------------
Lee_wan
Obsidian | Level 7

Thanks.

Final, I use the code as below;

data a;
  length key $200.;
  if _N_=1 then do;
    if 0 then set sashelp.class;
    array c _character_;
    array n _numeric_;
    dcl hash h(ordered:'A',multidata:'Y');
    h.definekey("sex","key");
    do over c;
        h.definedata (vname(c));
    end;
    do over n;
       h.definedata (vname(n));
    end;
    h.definedone();
run;
ChrisNZ
Tourmaline | Level 20

> Final, I use the code as below;

 

Please do not post code as text. Use the appropriate icon.

Lee_wan
Obsidian | Level 7
Sorry, edited!
mkeintz
PROC Star

You're making a character array and numeric array as a device to get variable names.  You can avoid that with the "call vnext()" call routine:

 

data _null_;
  set sashelp.class end=end_of_class;
  length key $200;
  length vname $32;
  if _n_=1 then do;
    dcl hash h (ordered:'A',multidata:'Y');
      h.definekey('name','key');

      do while (vname^='vname');
        call vnext(vname);
	    h.definedata(vname);
      end;
      h.definedone();
  end;
  *... other code ...;
run;

Make sure the first instance of KEY precedes the first reference to VNAME.  That will force the call vnext to encounter key before vname.

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

--------------------------
Lee_wan
Obsidian | Level 7
Oh!
Learned new knowledge! Thanks!
Lee_wan
Obsidian | Level 7
data _null_;
  set sashelp.class end=end_of_class;
  length key $200;
  length vname $32;
  if _n_=1 then do;
    dcl hash h (ordered:'A',multidata:'Y');
      h.definekey('name','key');

      do while (vname^='key');
        call vnext(vname);
            if vname ne 'end_of_class' then 
	    h.definedata(vname);
      end;
      h.definedone();
  end;
  *... other code ...;
run;

I made a small change as shown above.

Tom
Super User Tom
Super User

In addition to the fact that the load from dataset method would have no way to find a variable that doesn't exist there is also the problem that the hash object does not like to store records with missing values for one of the key variables. Where would the value of this new KEY variable come from?  Either create it in advance (possible with a view) or just load the observations explicitly in your data step that is creating the hash object.

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