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

I have a data that looks like the table below. I want to flag the same addresses occurring on the same date as an event.

This is the code I used but SAS is treating address as numeric and character at the same time. Any help will be appreciated.

 

Store IDAddressDateCustomer Name
1T6H 4M51-Jan-19John
1T6H 4M51-Jan-19Mary
1T6H 1R13-Mar-18Ami
1T6H 3M13-Mar-18Emi
1T6H 1A22-Feb-17Dan
1T6H 1A22-Feb-18Trish
1T6K 1G2 1-Jan-19Ivy
1T6H 1N24-Jan-19Dex
1T6H 1N23-Dec-18May
1T6H 4M52-Mar-17Nate
::::
1T6H 4M55-Mar-19Hans

 

proc sort data=have;
by address date;
run;

 

data want;

do until (last.date);

set have;

by address date;

if address then has_address=1;

if date then has_date=1;

end;

 

flag=0;

do until (last.date);

set want;

by address and date;

if has_address and has_date the output;

end;

drop has_:

run;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
infile cards expandtabs truncover;
input StoreID	Address & $10.	Date :date8.	CustomerName $;
format date date9.;
cards;
1	T6H 4M5	 1-Jan-19	John
1	T6H 4M5	 1-Jan-19	Mary
1	T6H 1R1	 3-Mar-18	Ami
1	T6H 3M1	 3-Mar-18	Emi
1	T6H 1A2	 2-Feb-17	Dan
1	T6H 1A2	 2-Feb-18	Trish
1	T6K 1G2  1-Jan-19	Ivy
1	T6H 1N2	 4-Jan-19	Dex
1	T6H 1N2	 3-Dec-18	May
1	T6H 4M5	 2-Mar-17	Nate
1	T6H 4M5	 5-Mar-19	Hans
;


proc sql;
create table want(drop=m) as
select *,count(*)>1 as flag,monotonic() as m
from have
group by StoreID,Address,date
order by m;
quit;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Hi @Lordy   Can you please post the sample of the OUTPUT you want for the sample INPUT you posted ?

novinosrin
Tourmaline | Level 20

Please post your results that you want that flag thing?

Lordy
Obsidian | Level 7
flag=1 for all addresses that has the same date
flag=0 otherwise
Lordy
Obsidian | Level 7
First two rows will have flag=1
novinosrin
Tourmaline | Level 20

data have;
infile cards expandtabs truncover;
input StoreID	Address & $10.	Date :date8.	CustomerName $;
format date date9.;
cards;
1	T6H 4M5	 1-Jan-19	John
1	T6H 4M5	 1-Jan-19	Mary
1	T6H 1R1	 3-Mar-18	Ami
1	T6H 3M1	 3-Mar-18	Emi
1	T6H 1A2	 2-Feb-17	Dan
1	T6H 1A2	 2-Feb-18	Trish
1	T6K 1G2  1-Jan-19	Ivy
1	T6H 1N2	 4-Jan-19	Dex
1	T6H 1N2	 3-Dec-18	May
1	T6H 4M5	 2-Mar-17	Nate
1	T6H 4M5	 5-Mar-19	Hans
;


proc sql;
create table want(drop=m) as
select *,count(*)>1 as flag,monotonic() as m
from have
group by StoreID,Address,date
order by m;
quit;
Lordy
Obsidian | Level 7
Worked as magic 🙂
Thank you

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1780 views
  • 0 likes
  • 2 in conversation