BookmarkSubscribeRSS Feed
SAShole
Pyrite | Level 9

Hi SAS friends,

I was wondering if it is possible to merge on a higher level variable simultaneously. for example to merge on state and county in one step (all state codes are built into county codes).

data County_Pop;

  input State :$2. County :$5. cName :$25. Population;

  format Population Comma16.;

  datalines;

01 01001 Autauga 222135

01 01011 Bullock 300000

01 01023 Choctaw 115000

42 42003 Allegheny 389471

42 42101 Philadelphia 2689765

;;;;

run;

data County_HH;

  input State :$2. County :$5. cName :$25. Households;

  format Households Comma16.;

  datalines;

01 01001 Autauga 106625

01 01011 Bullock 144000

01 01023 Choctaw 55200

42 42003 Allegheny 186946

42 42101 Philadelphia 1291087

run;

data State_Names;

  input State :$2. sName $25.;

  datalines;

01 Alabama

42 Pennsylvania

run;

data want;

  merge County_HH County_Pop State_Names;

  by ??? ;

run;

5 REPLIES 5
ballardw
Super User

Since both of your County_Hh and County_Pop data sets contain  the "county", which I recognize as FIPS or whatever they changed the name to codes, and a State code I would instead use the SAS supplied FIPSTATE or FIPNAMEL function to create the desired text instead of bothering to merge a third dataset.

Add code:

sName = FIPNAMEL(state);

to the data set merging the two sets.

SAShole
Pyrite | Level 9

Thanks ballardw. i never knew about the FIPNAMEL function. Do you know if there is a similar function for Census Region/Division?

ballardw
Super User

No function I am aware of. However if you can get any list of code values with text you want to display it is not very difficult to create a custom format that will display the desired value OR use a Put statement to create a new variable. Look up Proc Format and a CNTLIN dataset.

Hash tables and/or sql joins are also another way to merge data if you can get the codes and values in a dataset.

AhmedAl_Attar
Ammonite | Level 13

I developed the attached SAS macro (util_addCensusRegnDiv.sas) sometime ago, and here is a usage example

/* ***************************************************** */

/* Add Census Regions and Divisions of the United States */

/* ***************************************************** */

%util_addCensusRegnDiv( p_dsName=county_HH, p_stateVarName=%str(input(state,$2.)) );

Hope this helps,

Ahmed

Vish33
Lapis Lazuli | Level 10

Hi ,

I am not aware of this functions mentioned above by ballardw..i did normal merge on your data to map the state names and codes...

proc sort data=County_Pop;by State County cName; run;

proc sort data=County_HH;by State County cName; run;

proc sort data=State_Names;by State; run;

data want ;

merge County_Pop(in=a) County_HH(in=b) State_Names (in=c);

by State ;

if a and b;

run;

By running the above code i got the o/p as follows

StateCountycNamePopulationHouseholdssName
0101001Autauga222,135106,625Alabama
0101011Bullock300,000144,000Alabama
0101023Choctaw115,00055,200Alabama
4242003Allegheny389,471186,946Pennsylvania
4242101Philadelphia2,689,7651,291,087Pennsylvania

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1597 views
  • 4 likes
  • 4 in conversation