Pyrite | Level 9

## set by

Good morning. I can't understand why from this code the result on table "out" of first_unique is always 1

``````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

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: set by

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;```
2 REPLIES 2
Super User

## Re: set by

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.

Super User

## Re: set by

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;```
Discussion stats
• 2 replies
• 232 views
• 1 like
• 3 in conversation