04-16-2015 01:22 AM
I have the following dataset called "Agearank" containing 2,042 observations.
I also have another dataset called "Agea" containing 28,031 observations.
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.
04-16-2015 10:44 AM
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
(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;
04-17-2015 08:17 PM
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!
04-23-2015 03:48 PM
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:
merge Agearank (in=in_rank) Agea (keep=IN_PUMA_ID PWPUMAS);
04-16-2015 10:45 AM
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?