How to retrieve the particular element of hash object , let's say 10th element from hash object of sashelp.class?
data work.test;
if 0 then
set sashelp.class;
if _n_=1 then
do;
dcl hash h(dataset:'sashelp.class',ordered:'A',multidata:'yes');
dcl hiter hi('h');
h.definekey('Name');
h.definedata(all:'y');
h.definedone();
end;
/*rc=h.output(dataset:'work.out');*/
rc=hi.first();
rc1=hi.next();
put rc;
put rc1;
run;
You will need to count yourself in this case, unless you have embedded a counter when loading into the Hash.
data work.test;
if 0 then
set sashelp.class;
if _n_=1 then
do;
dcl hash h(dataset:'sashelp.class',ordered:'A',multidata:'yes');
dcl hiter hi('h');
h.definekey('Name');
h.definedata(all:
'y');
h.definedone();
end;
do rc=hi.first() by 0 while (rc=0);
n+1;
if n=10 then
do;
output;
stop;
end;
rc=hi.next();
end;
run;
You will need to count yourself in this case, unless you have embedded a counter when loading into the Hash.
data work.test;
if 0 then
set sashelp.class;
if _n_=1 then
do;
dcl hash h(dataset:'sashelp.class',ordered:'A',multidata:'yes');
dcl hiter hi('h');
h.definekey('Name');
h.definedata(all:
'y');
h.definedone();
end;
do rc=hi.first() by 0 while (rc=0);
n+1;
if n=10 then
do;
output;
stop;
end;
rc=hi.next();
end;
run;
@Haikuo Thanks. I was trying if I could use "h.num_items" method to get the desired result.
data work.test;
if 0 then
set sashelp.class;
if _n_=1 then
do;
dcl hash h(dataset:'sashelp.class',ordered:'A',multidata:'yes');
dcl hiter hi('h');
h.definekey('Name');
h.definedata(all:
'y');
h.definedone();
end;
num=h.num_items;
rc=hi.first();
do i= 1 to num;
if i=10 then output;
stop;
rc=hi.next();
end;
run;
Yes, you CAN use h.num_items. Fix your buggy code first before you can see it.
You have fixed a typo, which is good, but that was not what I meant. Now challenge yourself, why your code does not give you any output? if you check 'num', it has the value of 19.
Thanks, Haikuo. I will try to figure it out.
Check SETCUR() method. But you need a index variable for it .
data class;
set sashelp.class;
n+1;
run;
data work.test;
if _n_=1 then do;
if 0 then set class;
declare hash h(dataset:'class',ordered:'A');
declare hiter hi('h');
h.definekey('n');
h.definedata(all:'y');
h.definedone();
end;
rc=hi.setcur(key:10);
do while(rc=0);
output;
rc=hi.next();
end;
run;
Thanks, Ksharp. It gives 10 elements of hash table. Was looking for only 10th element.
data class;
set sashelp.class;
n+1;
run;
data work.test;
if _n_=1 then do;
if 0 then set class;
declare hash h(dataset:'class',ordered:'A');
declare hiter hi('h');
h.definekey('n');
h.definedata(all:'y');
h.definedone();
end;
rc=hi.setcur(key:10);
output;
stop;
run;
@Ksharp, If you already have the index loaded, you don't need hiter at all. Just 'rc=h.find(key:10)' will be sufficient.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.