BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10
data have; input loan_num $ date1 date9.; format date1 date9.; datalines; 111 11jun2019 111 11jul2019 112 11aug2019 113 11sep2019 113 11oct2019 114 11oct2019 ;run; Produces 111 11jun2019 111 11jul2019 112 11aug2019 113 11sep2019 113 11oct2019 114 11oct2019 I need to assign a row number to each distinct Loan_num such as loan_num date1 Row_Num 111 11Jun2019 1 111 11Jul2019 0 112 11Aug2019 1 113 11Sep2019 1 113 11Oct2019 0 114 11Oct2019 1 If the loan_num only appears once, assign a 1. If the loan_num appears twice, assign a 1 to the first one and a 0 to any subsequent loan_num
1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try below code

 

data have; 
input loan_num $ date1 date9.; 
format date1 date9.; datalines; 
111 11jun2019 
111 11jul2019 
112 11aug2019 
113 11sep2019 
113 11oct2019 
114 11oct2019 
;
run; 

proc sort data=have;
by loan_num date1;
run;

data want;
set have;
by loan_num date1;
if first.loan_num then Row_Num=1;
else Row_Num=0;
run;
Thanks,
Jag

View solution in original post

3 REPLIES 3
Q1983
Lapis Lazuli | Level 10
loan_num date1 Row_Num
111 11Jun2019 1
111 11Jul2019 0
112 11Aug2019 1
113 11Sep2019 1
113 11Oct2019 0
114 11Oct2019 1
Jagadishkatam
Amethyst | Level 16

Please try below code

 

data have; 
input loan_num $ date1 date9.; 
format date1 date9.; datalines; 
111 11jun2019 
111 11jul2019 
112 11aug2019 
113 11sep2019 
113 11oct2019 
114 11oct2019 
;
run; 

proc sort data=have;
by loan_num date1;
run;

data want;
set have;
by loan_num date1;
if first.loan_num then Row_Num=1;
else Row_Num=0;
run;
Thanks,
Jag
Kurt_Bremser
Super User

PLEASE do not (and I mean NOT) post code into the main posting window, or c**p like this happens:


@Q1983 wrote:
data have; input loan_num $ date1 date9.; format date1 date9.; datalines; 111 11jun2019 111 11jul2019 112 11aug2019 113 11sep2019 113 11oct2019 114 11oct2019 ;run; Produces 111 11jun2019 111 11jul2019 112 11aug2019 113 11sep2019 113 11oct2019 114 11oct2019 I need to assign a row number to each distinct Loan_num such as loan_num date1 Row_Num 111 11Jun2019 1 111 11Jul2019 0 112 11Aug2019 1 113 11Sep2019 1 113 11Oct2019 0 114 11Oct2019 1 If the loan_num only appears once, assign a 1. If the loan_num appears twice, assign a 1 to the first one and a 0 to any subsequent loan_num

Use the "little running man" instead, it's next to the </> marked here:Bildschirmfoto 2020-04-07 um 08.32.59.png

It's not rocket science, and it won't make your head explode. Promised.

 

To your question:

sort the data by loan_num and whatever you want as an additional sort order, and then, in a subsequent data step, do

by loan_num;
newvar = first.loan_num;

That's all.

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

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
  • 3 replies
  • 999 views
  • 2 likes
  • 3 in conversation