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

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 656 views
  • 2 likes
  • 2 in conversation