data tmp1;
input anagrafica_soggetto_id dt_denuncia_id :ddmmyy10.;
format dt_denuncia_id ddmmyy10.;
datalines;
25632 16/06/2016
4312 22/06/2015
4312 23/02/2016
4312 24/05/2021
run;
proc sort data=tmp1;
by ANAGRAFICA_SOGGETTO_ID dt_denuncia_id ;
run;
data out;
set tmp1;
by ANAGRAFICA_SOGGETTO_ID dt_denuncia_id ;
if first.dt_denuncia_id then first_unique = 1;
else first_unique = 0;
run;
Using set + by I would expect this result:
4312 22/06/2015 1
4312 23/02/2016 1
4312 24/05/2021 0
25632 16/06/2016 0
When in doubt look at the actual values of all of the first and last variables created.
data out; set tmp1; by ANAGRAFICA_SOGGETTO_ID dt_denuncia_id ; ANAGRAFICA_byfirst = first.ANAGRAFICA_SOGGETTO_ID; ANAGRAFICA_bylast = last.ANAGRAFICA_SOGGETTO_ID; denuncia_byfirst = first.dt_denuncia_id ; denuncia_bylast = last.dt_denuncia_id ; run;
What is it that you are trying to flag?
So you have four observations that you are ordering by two variables.
anagrafica_ dt_denuncia_ Obs soggetto_id id 1 4312 2015-06-22 2 4312 2016-02-23 3 4312 2021-05-24 4 25632 2016-06-16
Observation 1 is the first since it is the first observation overall.
Observation 2 is the first since the value of the second variable changes.
Observation 3 is the first since the value of the second variable changes.
Observation 4 is the first since the value of the first variable changes.
When in doubt look at the actual values of all of the first and last variables created.
data out; set tmp1; by ANAGRAFICA_SOGGETTO_ID dt_denuncia_id ; ANAGRAFICA_byfirst = first.ANAGRAFICA_SOGGETTO_ID; ANAGRAFICA_bylast = last.ANAGRAFICA_SOGGETTO_ID; denuncia_byfirst = first.dt_denuncia_id ; denuncia_bylast = last.dt_denuncia_id ; run;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: