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

I need to create a counter variable which does not have to be sorted. It cannot be sorted as the order is custom on a number of different other criteria, essentially it looks random but isn't in the larger dataset. The output counter would look like below using VAR_1 as the key variable.

 

VAR_1

Counter

A

1

C

1

A

2

B

1

A

3

C

2

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input VAR_1 $ /;
cards;
A
1
C
1
A
2
B
1
A
3
C
2
;
run;
data want;
if _n_=1 then do;
 if 0 then set have;
 declare hash h();
 h.definekey('var_1');
 h.definedata('count');
 h.definedone();
end;
 set have;
 if h.find()=0 then do;count=count+1;h.replace();end;
  else do;count=1;h.add();end;
run;
 
proc print;run;

View solution in original post

5 REPLIES 5
Ksharp
Super User
data have;
input VAR_1 $ /;
cards;
A
1
C
1
A
2
B
1
A
3
C
2
;
run;
data want;
if _n_=1 then do;
 if 0 then set have;
 declare hash h();
 h.definekey('var_1');
 h.definedata('count');
 h.definedone();
end;
 set have;
 if h.find()=0 then do;count=count+1;h.replace();end;
  else do;count=1;h.add();end;
run;
 
proc print;run;

Astounding
PROC Star

@Ksharp glad you posted this.  I was thinking, "If only I knew hashing."  This is the final piece of evidence that I must learn it.

Ksharp
Super User
Sure. Hash Table is a programming tool you must learn. 

FreelanceReinh
Jade | Level 19

@Astounding wrote:

This is the final piece of evidence that I must learn it.

I can recommend it and I've just noticed that the book "Data Management Solutions Using SAS® Hash Table Operations: A Business Intelligence Case Study" by Paul Dorfman and Don Henderson has now been published. It was mentioned in this interesting paper by the authors, which they called "a preview of [that] SAS book".

 

When I started learning hash object programming there was only Michele M. Burlew's book "SAS® Hash Object Programming Made Easy". In October 2016 I wrote a customer review (titled "Valuable resource for intermediate and advanced SAS programmers") about it on a very well known online bookseller's website. Since then, the hash object has become one of my must-have tools.

 

novinosrin
Tourmaline | Level 20
data have;
input VAR_1 $ /;
cards;
A
1
C
1
A
2
B
1
A
3
C
2
;
run;

data want;
set have;
array t(3,2) _temporary_;
_k=rank(VAR_1);
_k2=whichn(_k, of t(*));
if _k2=0 then do;_n+1; t(_n,1)=_k;count=1;t(_n,2)=count;end;
else do;count=t(_k2,2)+1;t(_k2,2)=count;end;
drop _:;
run;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1367 views
  • 4 likes
  • 5 in conversation