BookmarkSubscribeRSS Feed
rohithverma
Obsidian | Level 7

have input dataset  like 

 

DATA K;
INPUT city:$ ;
DATALINES;
a
a
a
a
a
b
b
b
;
RUN;

 

i need output like 

 

DATA r;

set k;

city seq;
a 1
a 1
a 1
a 1
a 1
b 2
b 2
b 2
;
RUN;

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

If your data is sorted by city, then simply do

 

data r;
    set k;
    by city;
    if first.city then seq+1;
run;

 

If not then do

 

data r;
  if _n_=1 then do;
    dcl hash h();
    h.defineKey ("city");
    h.defineData ("seq");
    h.defineDone ();
  end;
 
  set K;

  if h.find() ne 0 then do;
     seq+1;h.add();
  end;
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 702 views
  • 2 likes
  • 2 in conversation