BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26
data want;
    set have;
    number=_n_;
run;
--
Paige Miller
ashish112
Fluorite | Level 6
Thanks for quick reply, let me tell you my issue in detail. I have a date column where date is not in order, so in the first occurrence of date I want 1, in second occurrence I want 2, there may be cases when the dates are same, so for that dates I want same number. Hope it is clear
Reeza
Super User

Use BY Groups then instead - in the future please state issues like this at the beginning. 

Tutorial is here:

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 


@ashish112 wrote:
Thanks for quick reply, let me tell you my issue in detail. I have a date column where date is not in order, so in the first occurrence of date I want 1, in second occurrence I want 2, there may be cases when the dates are same, so for that dates I want same number. Hope it is clear

 

r_behata
Barite | Level 11
proc sql;
create table test
as select air,date,monotonic() as num
from sashelp.air
order by date;
quit;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would be very careful with the monotonic() function.  It may not behave in the way you expect it to.  In very basic scenarios it may be an option, however in most I would avoid using it at all.  The datastep version (as @PaigeMiller posted) is simpler and will work as expected.

ashish112
Fluorite | Level 6
Thanks for quick reply, let me t iell you my issue in detail. I have a date column where date is not in order, so in the first occurrence of date I want 1, in second occurrence I want 2, there may be cases when the dates are same, so for that dates I want same number. Hope it is clear

Shmuel
Garnet | Level 18

If I understand correctly, you need first step to sort your data by date then use the proposed solution;

what kind of date is  '########' ? In case of sort those dates will be either at start or at end depends on OS.

proc sort data=have; by date; run;
data want;
 set have;
number = _N_;
run;
Astounding
PROC Star

Looking at the example you posted, sorting your data is out of the question.  Instead, use this variation:

 

data want;

set have;

by date notsorted;

if first.date then number + 1;

run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 22 replies
  • 2755 views
  • 4 likes
  • 8 in conversation