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 [email protected]
josh 1/2/2015 [email protected]
josh 1/3/2015 [email protected]
mary 1/4/2015 [email protected]
mary 1/5/2015 [email protected]
josh 1/6/2015 [email protected]
josh 1/7/2015 [email protected]
josh 1/8/2015 [email protected]
mary 1/9/2015 [email protected]
mary 1/10/2015 [email protected]

 

ID Date email_address distinct_emails_for_user first_time_email_seen_with_user

josh 1/1/2015 [email protected] 1 .
josh 1/2/2015 [email protected] 2 YES
josh 1/3/2015 [email protected] 3 YES
mary 1/4/2015 [email protected] 1 .
mary 1/5/2015 [email protected] 2 YES
josh 1/6/2015 [email protected] 3 NO
josh 1/7/2015 [email protected] 3 NO
josh 1/8/2015 [email protected] 3 NO
mary 1/9/2015 [email protected] 2 NO
mary 1/10/2015 [email protected] 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; 

AI Navigator GIF.gif


Navigate AI governance with SAS tools for compliance and ethical AI practices. Try SAS Ai Navigator for free for the first six months.

Check it out on Microsoft Marketplace →

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
  • 2105 views
  • 1 like
  • 2 in conversation