Example: Human Traffic of Stadium
We can use left join to take look the amount of a date, its next two dates, and its previous two dates. The SAS code is as follows:
proc sql;
create table out as
select a.id, a.date, a.people
from stadium as a left join stadium as a1
on a.date = a1.date-1
left join stadium as a2
on a.date = a2.date-2
left join stadium as a3
on a.date = a3.date+1
left join stadium as a4
on a.date = a4.date+2
where (a.people >= 100 and a1.people >= 100 and a2.people >= 100) or
(a.people >= 100 and a3.people >= 100 and a4.people >= 100) or
(a.people >= 100 and a1.people >= 100 and a3.people >= 100);
quit;
proc print data=out noobs;
format date YYMMDD10.;
run;
X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people
Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).
For example, the table stadium
:+------+------------+-----------+ | id | date | people | +------+------------+-----------+ | 1 | 2017-01-01 | 10 | | 2 | 2017-01-02 | 109 | | 3 | 2017-01-03 | 150 | | 4 | 2017-01-04 | 99 | | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-08 | 188 | +------+------------+-----------+
For the sample data above, the output is:
+------+------------+-----------+ | id | date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-08 | 188 | +------+------------+-----------+
We can use left join to take look the amount of a date, its next two dates, and its previous two dates. The SAS code is as follows:
proc sql;
create table out as
select a.id, a.date, a.people
from stadium as a left join stadium as a1
on a.date = a1.date-1
left join stadium as a2
on a.date = a2.date-2
left join stadium as a3
on a.date = a3.date+1
left join stadium as a4
on a.date = a4.date+2
where (a.people >= 100 and a1.people >= 100 and a2.people >= 100) or
(a.people >= 100 and a3.people >= 100 and a4.people >= 100) or
(a.people >= 100 and a1.people >= 100 and a3.people >= 100);
quit;
proc print data=out noobs;
format date YYMMDD10.;
run;
Comments
Post a Comment