BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AmirSari
Quartz | Level 8

Hi all,

 

Below is a sample of my dataset. I want to create a dummy variable for the auditor’s first time audit with the client

 

Client_id year auditor_id dummy
1 1990 6 1
1 1991 6 0
1 1992 4 1
1 1993 4 0
1 1994 8 1
1 1995 8 0
1 1996 4 0
1 1997 4 0
2 1990 5 1
2 1991 5 0
2 1992 5 0
2 1993 5 0
2 1994 5 0
2 1995 5 0  

 

dummy takes 1 for the first observation of each Client_id . For other observations dummy takes 1 only if it is the auditor’s first time audit with the client.

 

Thanks in advance for your help!  

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
It's easy if you sort your data:

proc sort data=have;
by client_id auditor_ud year;
run;

data want;
set have;
by client_id auditor_id;
dummy = first.auditor_id;
run;

If you want to, sort your data again to put it back in its original order.

View solution in original post

4 REPLIES 4
Astounding
PROC Star
It's easy if you sort your data:

proc sort data=have;
by client_id auditor_ud year;
run;

data want;
set have;
by client_id auditor_id;
dummy = first.auditor_id;
run;

If you want to, sort your data again to put it back in its original order.
Ksharp
Super User
data have;
input Client_id year auditor_id dummy	;
cards;
1 1990 6 1
1 1991 6 0
1 1992 4 1
1 1993 4 0
1 1994 8 1
1 1995 8 0
1 1996 4 0
1 1997 4 0
2 1990 5 1
2 1991 5 0
2 1992 5 0
2 1993 5 0
2 1994 5 0
2 1995 5 0  
;
data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h();
  h.definekey('auditor_id');
  h.definedone();
 end;
set have;
by Client_id;
if first.Client_id then h.clear();
if h.check()=0 then want=0;
 else do;want=1; h.add();end;
run;
AmirSari
Quartz | Level 8

Thanks, Ksharp! Your solution works too!

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 562 views
  • 1 like
  • 3 in conversation