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

Hi all,

I’d like to ask if you can help on this. I would like to know the numbers of visits one year before the date of ‘time_threshold’in table one, but not including the visit on the date of time_threshold. For example, the first person’s time_threshold is 20050917, then the number of visits 12 months before this date is 2 times (not including the 'time_threshold' visit). Table two includes all records in terms of visit time by each person. 

The table that I am looking for is as follows:

Id

Counts

1

2

2

1

3

0

4

1

data one;

input id time_threshold yymmdd10.;

format time_threshold yymmdd10.;

datalines;

1 20050917

2 20071005

3 20050606

4 20090116

;

run;

data two;

input id visit_time yymmdd10.;

format visit_time yymmdd10.;

datalines;

1 20041212

1 20031105

1 20050917

1 20050407

1 20050112

2 20071005

2 20090301

2 20061215

2 20110915

3 20050606

3 20050918

4 20101112

4 20090116

4 20080909

;

run;

Thanks  a lot for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

it should be count(visit_time) rather than count(*) actually....

And 1 looks, correct, three visits:

2004-12-12

2005-01-12

2005-04-07

See the full details before the filter/sort.

proc sql;

create table summary as

select one.id, count(visit_time) as num_visits, *, time_threshold-visit_time as diff

from one

left join two

on one.id=two.id;

/*and one.time_threshold-two.visit_time between 1 and 365*/

/*group by one.id;*/

quit;

View solution in original post

4 REPLIES 4
Reeza
Super User

proc sql;

create table summary as

select one.id, count(*) as num_visits

from one

left join two

on one.id=two.id

and one.time_threshold-two.visit_time between 1 and 365

group by one.id;

quit;

user1
Calcite | Level 5

Thanks very much for your prompt reply! I run the code, but the results for person 1 and 3 did not look correct. For example, person 3 should have 0 visit, but the result showed 1 visit for this person.

Reeza
Super User

it should be count(visit_time) rather than count(*) actually....

And 1 looks, correct, three visits:

2004-12-12

2005-01-12

2005-04-07

See the full details before the filter/sort.

proc sql;

create table summary as

select one.id, count(visit_time) as num_visits, *, time_threshold-visit_time as diff

from one

left join two

on one.id=two.id;

/*and one.time_threshold-two.visit_time between 1 and 365*/

/*group by one.id;*/

quit;

user1
Calcite | Level 5

Thanks so much! I've got the right table I wanted.

Best regards.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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