BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

9 REPLIES 9
Haikuo
Onyx | Level 15

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;
SAS_inquisitive
Lapis Lazuli | Level 10

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

 

Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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.

SAS_inquisitive
Lapis Lazuli | Level 10

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

Ksharp
Super User

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;
SAS_inquisitive
Lapis Lazuli | Level 10

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

Ksharp
Super User
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;
Haikuo
Onyx | Level 15

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 1526 views
  • 1 like
  • 3 in conversation