DATA Step, Macro, Functions and more

Element of hash object

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Element of hash object

[ Edited ]

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;

Accepted Solutions
Solution
‎01-05-2016 09:50 PM
Respected Advisor
Posts: 3,156

Re: Element of hash object

Posted in reply to SAS_inquisitive

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;

View solution in original post


All Replies
Solution
‎01-05-2016 09:50 PM
Respected Advisor
Posts: 3,156

Re: Element of hash object

Posted in reply to SAS_inquisitive

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;
Super Contributor
Posts: 271

Re: Element of hash object

[ Edited ]

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

 

Respected Advisor
Posts: 3,156

Re: Element of hash object

Posted in reply to SAS_inquisitive

Yes, you CAN use h.num_items. Fix your buggy code first before you can see it.

Respected Advisor
Posts: 3,156

Re: Element of hash object

Posted in reply to SAS_inquisitive

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.

Super Contributor
Posts: 271

Re: Element of hash object

Thanks, Haikuo.  I will try to figure it out.

Super User
Posts: 10,041

Re: Element of hash object

Posted in reply to SAS_inquisitive

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;
Super Contributor
Posts: 271

Re: Element of hash object

Thanks, Ksharp.  It gives 10 elements of hash table. Was looking for only 10th element.

Super User
Posts: 10,041

Re: Element of hash object

[ Edited ]
Posted in reply to SAS_inquisitive
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;
Respected Advisor
Posts: 3,156

Re: Element of hash object

@Ksharp, If you already have the index loaded, you don't need hiter at all. Just 'rc=h.find(key:10)' will be sufficient.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 349 views
  • 1 like
  • 3 in conversation