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

Greetings,

 

I'm modifying this code to attempt to obtain the following output. Basically I have a first_time_email_seen_with_user field below, and I want to to return a blank for this value.

 

ID Date email_address

josh 1/1/2015 josh1@gmail.com
josh 1/2/2015 josh2@yahoo.com
josh 1/3/2015 josh3@yahoo.com
mary 1/4/2015 mary123@aol.com
mary 1/5/2015 mars@blah.com
josh 1/6/2015 josh1@gmail.com
josh 1/7/2015 josh2@yahoo.com
josh 1/8/2015 josh3@yahoo.com
mary 1/9/2015 mary123@aol.com
mary 1/10/2015 mars@blah.com

 

ID Date email_address distinct_emails_for_user first_time_email_seen_with_user

josh 1/1/2015 josh1@gmail.com 1 .
josh 1/2/2015 josh2@yahoo.com 2 YES
josh 1/3/2015 josh3@yahoo.com 3 YES
mary 1/4/2015 mary123@aol.com 1 .
mary 1/5/2015 mars@blah.com 2 YES
josh 1/6/2015 josh1@gmail.com 3 NO
josh 1/7/2015 josh2@yahoo.com 3 NO
josh 1/8/2015 josh3@yahoo.com 3 NO
mary 1/9/2015 mary123@aol.com 2 NO
mary 1/10/2015 mars@blah.com 2 NO

 

So far ksharp has assisted me with this portion of the code. I know that there's something that we have to add with first.id -- that will allow me to modify the first instance of the record occuring, but I'm unsure of how where to implement this.

 

 

data want;
 if _n_ eq 1 then do;
  declare hash hh();
  hh.definekey('id');
  hh.definedata('count');
  hh.definedone();
  declare hash h();
  h.definekey('id','email_address');
  h.definedone();
 end;
 set have;
 length flag $ 8; 
 if h.find()=0 then do;
   hh.find();flag='NO';
 end;
 else do;
  flag='YES';
  if hh.find()=0 then count+1;
   else count=1;
 h.replace();hh.replace();
 end;
run; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
JS
Obsidian | Level 7 JS
Obsidian | Level 7

 

data want4 (drop=flag);
set want2;
/*by id;*/
first_time_appear=flag;
if first.id then first_app_in_acct =1;
run;
proc print;quit;

 

I added an extra field at the end that created a seperate flag for the first record by account. I simply use that to exclude those values when I am tabulating the query.

View solution in original post

2 REPLIES 2
JS
Obsidian | Level 7 JS
Obsidian | Level 7

 

data want4 (drop=flag);
set want2;
/*by id;*/
first_time_appear=flag;
if first.id then first_app_in_acct =1;
run;
proc print;quit;

 

I added an extra field at the end that created a seperate flag for the first record by account. I simply use that to exclude those values when I am tabulating the query.

Ksharp
Super User

Overwrite it at the end of code.

 

data want;
 if _n_ eq 1 then do;
  declare hash hh();
  hh.definekey('id');
  hh.definedata('count');
  hh.definedone();
  declare hash h();
  h.definekey('id','email_address');
  h.definedone();
 end;
 set have;
 length flag $ 8; 
 if h.find()=0 then do;
   hh.find();flag='NO';
 end;
 else do;
  flag='YES';
  if hh.find()=0 then count+1;
   else count=1;
 h.replace();hh.replace();
 end;


if count=1 then flag=' ';

run; 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1145 views
  • 1 like
  • 2 in conversation