DATA Step, Macro, Functions and more

Modify output occurring

Accepted Solution Solved
Reply
Contributor JS
Contributor
Posts: 38
Accepted Solution

Modify output occurring

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; 

 


Accepted Solutions
Solution
‎04-26-2016 09:46 PM
Contributor JS
Contributor
Posts: 38

Re: Modify output occurring

 

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


All Replies
Solution
‎04-26-2016 09:46 PM
Contributor JS
Contributor
Posts: 38

Re: Modify output occurring

 

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.

Super User
Posts: 10,023

Re: Modify output occurring

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; 
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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