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

Hello,

 

Say that I have a dataset1 with ID and variable 1, like this:

IDVar1
A5.1
B6.1
C7.1
D8.1
F9.1
G10.1

 

I also have dataset2, which is a reference table like this:

Var2Var3
520
630
740
850
960

 

How do I obtain a dataset that has ID, Var1, and Var3 such that Var3 corresponds to Var1's closest match in Var2 of the reference table. The resulting dataset would look like this:

IDVar1Var3
A5.120
B6.130
C7.140
D8.150
F9.160
G10.160

 

I came across the following article but I'm not sure how it can be adapted to my problem: http://www.lexjansen.com/pharmasug/2003/CodersCorner/cc001.pdf

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

proc sql;
	CREATE TABLE want0 AS
	SELECT a.ID, a.VAR1, b.VAR2, b.VAR3, abs(b.VAR2-a.VAR1) AS DIFF, min(CALCULATED DIFF) AS MIN_DIFF
	FROM have1 a, have2 b
	GROUP BY a.ID
	HAVING CALCULATED DIFF=CALCULATED MIN_DIFF;
quit;

View solution in original post

2 REPLIES 2
gamotte
Rhodochrosite | Level 12

Hello,

 

proc sql;
	CREATE TABLE want0 AS
	SELECT a.ID, a.VAR1, b.VAR2, b.VAR3, abs(b.VAR2-a.VAR1) AS DIFF, min(CALCULATED DIFF) AS MIN_DIFF
	FROM have1 a, have2 b
	GROUP BY a.ID
	HAVING CALCULATED DIFF=CALCULATED MIN_DIFF;
quit;
Reeza
Super User

Have you tried the SQL solution in that paper? It's probably the easiest to implement if you're new to SAS.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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