BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
 
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
ballardw
Super User

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;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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.

ballardw
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 220 views
  • 1 like
  • 3 in conversation