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
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;
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 🙂
Draycut,
Sorry for my poor presentation.
I've modified a little bit.
Hope it works.
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;
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.
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.
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.