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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1087 views
  • 4 likes
  • 5 in conversation