data want;
set dep5 nobs=nobs;
want=nobs+1-_n_;
run;
please try
data have ;
input Top Date: mmddyy10. ;
format date date9.;
cards;
1 02/01/2018
2 02/05/2018
3 03/01/2018
;
proc sort data=have ;
by descending date;
run;
data want;
set have;
by descending date ;
retain count;
if first.date then count+1;
run;
First you sort your data by descending and count the records by id and then sort back to ascending.
data have;
format date date9.;
infile datalines dlm=" ";
input ID $ Date date9. ;
datalines;
A 01MAR2018
A 02MAR2018
A 03MAR2018
B 01MAR2018
B 02MAR2018
B 03MAR2018
;
run;
proc sort data=have;
by id descending date;
run;
data want;
set have;
by id;
if first.id then count=1;
else count+1;
run;
proc sort data=want;
by id date;
proc print data=want;
run;
data have ;
input Top Date: mmddyy10. ;
format date date9.;
cards;
1 02/01/2018
2 02/05/2018
3 03/01/2018
;
data want;
set have nobs=nobs;
want=nobs+1-_n_;
run;
Proper sort order will solve your problem. Please provide with some actual sample data with few records.
data want;
set dep5 nobs=nobs;
want=nobs+1-_n_;
run;
proc sort data=dep5;
by top descending date;
run;
data dep5;
set dep5;
by top;
if first.top then count=1;
else count+1;
run;
proc sort data=dep5;
by top date;
proc print data=dep5;
run;
could you please try this code, it is the same I provided earlier
not sure if you tried earlier but if you could try it will help you
proc sort data=have ;
by descending date;
run;
data want;
set have;
by descending date ;
retain count;
if first.date then count+1;
run;
proc sort data=want;
by id date;
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.