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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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