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.
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;
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;
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;
> Final, I use the code as below;
Please do not post code as text. Use the appropriate icon.
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.
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.
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.