data want;
set have;
number=_n_;
run;
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
proc sql;
create table test
as select air,date,monotonic() as num
from sashelp.air
order by date;
quit;
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.
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;
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;
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!
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.