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

Hi Team,

I was wondering if I could get help to convert Rows to Columns. Below is the input table:

Location

 

Type Combined

 

Population

 

Collin   County, TX

 

Uninsured_Pop_Num

 

500

 

Collin   County, TX

 

Uninsured_Pop_Den

 

15000

 

Plano,   TX

 

Hispanic_Num

 

200

 

Plano,   TX

 

Hispanic_Den

 

10000

 

I would need to convert it to as follows:

Location

 

Type 

 

Numerator

 

Denominator

 

Collin   County, TX

 

Uninsured_Pop

 

500

 

15000

 

Plano,   TX

 

Hispanic_Pop

 

200

 

10000

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

Yes, see my example, I use this:

Collin County, TX~Hispanic_Num~100

Collin County, TX~Hispanic_Den~1000

Collin County, TX~Uninsured_Pop_Num~500

Collin County, TX~Uninsured_Pop_Den~15000

Plano, TX~Hispanic_Num~200

Plano, TX~Hispanic_Den~10000

And it works...

View solution in original post

4 REPLIES 4
FriedEgg
SAS Employee

data foo;

infile cards dsd dlm='~';

input (city_state type_combined) (:$256.) population;

type=prxchange('s/_num$|_den$//i',-1,strip(type_combined));

if not(prxmatch('/_pop$/i',strip(type))) then type=strip(type)||'_Pop';

cards;

Collin County, TX~Hispanic_Num~100

Collin County, TX~Hispanic_Den~1000

Collin County, TX~Uninsured_Pop_Num~500

Collin County, TX~Uninsured_Pop_Den~15000

Plano, TX~Hispanic_Num~200

Plano, TX~Hispanic_Den~10000

;

run;

data bar;

set foo;

by city_state type;

retain type numerator denominator;

if upcase(scan(type_combined,-1,'_'))='NUM' then numerator=population;

else denominator=population;

if last.type then output;

drop type_combined population;

run;

Tom
Super User Tom
Super User

You could use PROC TRANSPOSE.

data need ;

   set have ;

  _name_ = scan(type_combined,-1,'_');

  type=substr(type_combined,1,length(type_combined)-length(_name_)-1);

run;

proc transpose data=need out=want ;

   by location type;

   var population;

   id _name_;

run;

Roger
Calcite | Level 5

Thanks Tom and FriedEgg for your help!

Just curious - my City_State is repeated several times and could I still use same code to get all the the input rows?

Collin County, TX~Hispanic_Num~100

Collin County, TX~Hispanic_Den~1000

Collin County, TX~Uninsured_Pop_Num~500

Collin County, TX~Uninsured_Pop_Den~15000

Collin County, TX~Hispanic_Male_Num~200

Collin County, TX~Hispanic_Male_Den~1000

Collin County, TX~Uninsured_Female_Num~300

Collin County, TX~Uninsured_Female_Den~15000

Thanks again!

Roger

FriedEgg
SAS Employee

Yes, see my example, I use this:

Collin County, TX~Hispanic_Num~100

Collin County, TX~Hispanic_Den~1000

Collin County, TX~Uninsured_Pop_Num~500

Collin County, TX~Uninsured_Pop_Den~15000

Plano, TX~Hispanic_Num~200

Plano, TX~Hispanic_Den~10000

And it works...

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
  • 4 replies
  • 699 views
  • 8 likes
  • 3 in conversation