Solved
Contributor
Posts: 45

# Single variable matching

[ Edited ]

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

Accepted Solutions
Solution
‎06-22-2017 08:14 PM
Super User
Posts: 9,861

## Re: Single variable matching

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;```

All Replies
PROC Star
Posts: 1,405

## Re: Single variable matching

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

Contributor
Posts: 45

## Re: Single variable matching

[ Edited ]

Draycut,

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

Solution
‎06-22-2017 08:14 PM
Super User
Posts: 9,861

## Re: Single variable matching

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;```
Contributor
Posts: 45

## Re: Single variable matching

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)
Super User
Posts: 9,861

## Re: Single variable matching

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.

☑ This topic is solved.