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

Hi all,

 

I looking to pull geo data to a table via a left join. I have got to a point where I just want to join first matching result in my geo table to my original table. In oracle I would have used something like rownum < 2, is there an equivalent within SAS?

 

Below is an example of the join and conditions

 

LEFT JOIN
TMP_PRPN.COMPANIES_NACE_CODE_GEODIR2 t2
ON t1.join_status = 0
AND t1.geo_directory_grp = 'BUILDING'
AND t2.building_id = t1.geo_directory_id

AND t1.thorfare_id IS NOT MISSING
AND t2.thorfare_id = t1.thorfare_id
AND t2.nace_code LIKE ('Q.86.%')
AND prxchange('s/\b(DOCTOR)\b/ /o',-1,t2.organisation_name) =* cats(t1.firstname, t1.surname)

 

Any ideas?

 

Regards

Finbar

1 ACCEPTED SOLUTION

Accepted Solutions
fgillen1
Fluorite | Level 6

I have created a work around where I have created a unique ID field in the left hand column. Done my left join where it has created extra rows.

 

After this I just done a Group By based on the unique id and selected the max address_reference field from the right hand table. Complete!

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If there is nothing in the data which indicates the record is the "first record" then why not just select distinct () on the values you want to use so you only get one record per group.  You can force it to take this adhoc "first record", however its not straight-forward, and not really a good way to be selecting data (as each time you run it, depending on sort orders, indexes, new/deleted data etc.) you may get a different result.  Better to isolate the data you really want logically that way each time you run it it will always work the same.

fgillen1
Fluorite | Level 6
Building_ID is restricts to an individual building, so as I say for this analysis it will be perfectly fine. Could you send an example piece of code of how you would use it? I have populated fields within PL/SLQ using SELECT DISTINCT() but not in SAS.
LinusH
Tourmaline | Level 20

I assume it would be (very) similar.

Just search the PROC SQL documentation for DISTINCT.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

At a guess, the simplest method would be to add a sub-qeury:

LEFT JOIN
(select distinct BUILDING from TMP_PRPN.COMPANIES_NACE_CODE_GEODIR2) t2
ON t1.join_status = 0
AND t1.geo_directory_grp = 'BUILDING'
AND t2.building_id = t1.geo_directory_id
fgillen1
Fluorite | Level 6

That does exactly the same as the left join on the full table. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You will note the word guess on the first line of the post here.  You have not provided anything for me to work with.  If you want a good answer provide some test data in the form of a datastep, post it in the code window (its the {I} above post area), and show what you want out.

You asked "SELECT DISTINCT() but not in SAS." - I showed how this works, I cannot guess your data or process or what you want.

fgillen1
Fluorite | Level 6

I have created a work around where I have created a unique ID field in the left hand column. Done my left join where it has created extra rows.

 

After this I just done a Group By based on the unique id and selected the max address_reference field from the right hand table. Complete!

sas-innovate-2024.png

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.

 

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