BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chung-Li
Quartz | Level 8

Hi all, 

 

While I was doing age-matching recently, I found this was not that easy as I originally thought.

Therefore, I decide to post my question here for asking your help.

 

My data looks like:

(I only present age-matching needed data)

ID	Group	Age
1	1	50
2	2	45
3	1	55
4	1	67
5	1	80
6	2	90
7	2	70
8	1	50
9	2	45
10	2	80
.	.	.
.	.	.
.	.	.

What I want to do is age-matching for group1 and 2.

 

That is, for each patient in group 1, I want to find one (or many) patient in group 2 that the difference between these patients is less than a certain amount of range.

For example, the age of first patient in group 1 is 50, then I want to find a (or many) patient in group 2 whose age is between 50-5 and 50+5.  

 

Yet, I have difficulty achieveing this.

Hope you guys can give me a hand, thanks in advance!

 

PS. My sas version is 9.4

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep.

Something like (and no test data to run it on):

proc sql;
  create table WANT as
  select  A.*,
          B.ID as B_ID,
          B.AGE as B_AGE
  from    (select * from HAVE where GROUP=1) A
  left join (select * from HAVE where GROUP=2) B
  on        (B.AGE-5) <= A.AGE <= (B.AGE+5);
quit;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Step 1 and 2 are pretty straight forward. But I am having trouble understanding step 3. Perhaps you could explain what are trying to achieve and you want you final data to look like, this makes it a lot easier to help you 🙂

Chung-Li
Quartz | Level 8

Draycut,

Sorry for my poor presentation.
I've modified a little bit.
Hope it works.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep.

Something like (and no test data to run it on):

proc sql;
  create table WANT as
  select  A.*,
          B.ID as B_ID,
          B.AGE as B_AGE
  from    (select * from HAVE where GROUP=1) A
  left join (select * from HAVE where GROUP=2) B
  on        (B.AGE-5) <= A.AGE <= (B.AGE+5);
quit;
Chung-Li
Quartz | Level 8
RW9,

First, I want to thank you for helping me!

But, still, I have a question that why so many people use proc sql to solve matching problem?

Before I ask how to matching in here, I did search information about it. It turned almost all solutions require proc sql.

May I ask why do you prefer solving this with sql than sas itself?
(PS. I have no idea about what is sql at all, it looks like it worth learning if it's so powerful)
RW9
Diamond | Level 26 RW9
Diamond | Level 26

With Base SAS the merge statement merges two datasets by a given set of variables.  As the merge we want here is a condition rather than a merge then it is simpler to use SQL.  Also with SQL you don't have to explicitly sort each dataset.  

 

SQL is a query language which has been around more or less forever. It comes from Relational Databases where data is split up into many datasets with key identifiers which link the data back together - this format reduces the size of the data.  The query language is designed specifically to join all this data back together and return query results.  Its biggest benefit is the powerful merging.  You can do other interesting things like sub-querying and nesting.  Its worth having at least a basic knowledge of SQL, more important if you have to access databases as you can pass SQL code through to them.

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!

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
  • 5 replies
  • 1128 views
  • 2 likes
  • 3 in conversation