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

I have the following table and would like an indicator variable equal to one when there is at least one competitor in the same industry in the same state in the same year. Basically it is by competition in the same industry by state. Could you please let me know the SAS SQL Proc code:

Have table:

IDYearIndustrySTATE
11989111110CA
11990111110CA
11991111110CA
11992111110CA
21989111111AR
21990111111AR
21991111111AR
31989111110CA
31990111110CA
31991111110CA
31992111110CA
31993111110CA
31994111110CA
31995111110CA
31996111110CA

 

Want table:

IDYearIndustrySTATEDUMMY
11989111110CA1
11990111110CA1
11991111110CA1
11992111110CA1
21989111111AR0
21990111111AR0
21991111111AR0
31989111110CA1
31990111110CA1
31991111110CA1
31992111110CA1
31993111110CA0
31994111110CA0
31995111110CA0
31996111110CA0

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
You mean at least TWO competitor ?


data have;
infile cards expandtabs truncover;
input ID	Year	Industry	STATE $;
cards;
1	1989	111110	CA
1	1990	111110	CA
1	1991	111110	CA
1	1992	111110	CA
2	1989	111111	AR
2	1990	111111	AR
2	1991	111111	AR
3	1989	111110	CA
3	1990	111110	CA
3	1991	111110	CA
3	1992	111110	CA
3	1993	111110	CA
3	1994	111110	CA
3	1995	111110	CA
3	1996	111110	CA
;
run;
proc sql;
select *,case when(count(distinct id) ne 1) then 1 else 0 end as flag
 from have
  group by year,industry,state
   order by id,year,industry,state;
  
quit;


View solution in original post

2 REPLIES 2
Ksharp
Super User
You mean at least TWO competitor ?


data have;
infile cards expandtabs truncover;
input ID	Year	Industry	STATE $;
cards;
1	1989	111110	CA
1	1990	111110	CA
1	1991	111110	CA
1	1992	111110	CA
2	1989	111111	AR
2	1990	111111	AR
2	1991	111111	AR
3	1989	111110	CA
3	1990	111110	CA
3	1991	111110	CA
3	1992	111110	CA
3	1993	111110	CA
3	1994	111110	CA
3	1995	111110	CA
3	1996	111110	CA
;
run;
proc sql;
select *,case when(count(distinct id) ne 1) then 1 else 0 end as flag
 from have
  group by year,industry,state
   order by id,year,industry,state;
  
quit;


Agent1592
Pyrite | Level 9

Thank you, yes I mean at least two competitors.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 4133 views
  • 0 likes
  • 2 in conversation