BookmarkSubscribeRSS Feed
MichaelLin
Calcite | Level 5

I have the following dataset called "Agearank" containing 2,042 observations.

Capture.PNG

I also have another dataset called "Agea" containing 28,031 observations.

f.PNG

I would like to join "Agea" into "Agearank" by IN_PUMA_ID. My goal is to attach the variable PWPUMAS (i.e., Super-PUMA of work) in the "Agea" dataset to the "Agearank" dataset. In the newly created dataset (e.g., new), I would only like to have 2,042 observations based on the "Agearank" dataset. How should I write the SAS code? Thank you.

5 REPLIES 5
CTorres
Quartz | Level 8

There are many values of the variable PWPUMAS for each IN_PUMA_ID. You need to chose what of them to attach. Maybe MAX(PWPUMAS) ?

CTorres
Quartz | Level 8

Try this to attach the value of MAX(PWPUMAS) by each IN_PUMA_ID group:

proc sql noprint;

  create table new as

  select A.*, MPWPUMAS as PWPUMAS

  from Agearank A

  left join

    (select distinct IN_PUMA_ID, MAX(PWPUMAS) as MPWPUMAS

     from Agea B

     group by IN_PUMA_ID)

  on (A.IN_PUMA_ID = B.IN_PUMA_ID)

  order by 1;

quit;

CTorres

MichaelLin
Calcite | Level 5

Thank you for your reply.

Suppose PWPUMAS with values of 0 means the data are missing for those observations and let's assume we have deleted them from the dataset "Agea". Accordingly, each observation's PWPUMAS would have a unique value. If this is the case, how to write the SAS code for the question I asked? Thanks!

Cheers,

Michael

CTorres
Quartz | Level 8

The same code works OK with the new supposition: if all values of PWPUMAS are the same then MAX(PWPUMAS) is this value for each IN_PUMA_ID

You can also use the following code assuming both files are sorted by IN_PUMA_ID and PWPUMAS has the same value for each IN_PUMA_ID:

Data new;

  merge Agearank (in=in_rank) Agea (keep=IN_PUMA_ID PWPUMAS);

  by IN_PUMA_ID;

  if in_rank;

run;

CTorres

ballardw
Super User

WHICH value of PWPUMAS do you want from the second table? Your example data shows ID 0100100 with values of 300 and 0? What is the rule for selecting the single value of PWPUMAS from Agea when there are multiple values?

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 980 views
  • 0 likes
  • 3 in conversation