## Row to clumn

Solved
Super Contributor
Posts: 712

# Row to clumn

Hi

how to move rows to columns:

id       attr

1       phone1

1      phone2

2     phone1

4   email1

2          phone1

4                                                                             email1

Accepted Solutions
Solution
‎02-01-2013 06:27 PM
PROC Star
Posts: 8,163

## Re: Row to clumn

I can only guess at the variable lengths you might really need, but I think you are looking for something like:

data have;

informat attr \$80.;

input id attr &;

cards;

1  555-555-5555

1  (555) 555-5555

1  80 Here Street, Somewhere

2  1-555-555-5555

3  Apt 1A

;

set have;

by id;

if _n_ = 1 then pattern = prxparse("/\d\d\d-\d{4}/");

if first.id then do;

call missing(email);

call missing(phone);

end;

if PRXMATCH(pattern,attr) then phone=catx(',',phone,attr);

else if index(attr,"@") then email=catx(',',email,attr);

if last.id then output;

run;

All Replies
PROC Star
Posts: 8,163

## Re: Row to clumn

Tell us more!  Is your example really what your data look like or do the phone numbers look like typical phone numbers (and, if so, how many different types are there?  e.g. 555-555-5555, 1-555-555-5555, (555) 555-5555), is the @ symbol used in any other fields other than email addresses?

Or are you only asking regarding the specifics you initially posted?

Super Contributor
Posts: 712

## Re: Row to clumn

phone is typical phone numbers

and email has @

Solution
‎02-01-2013 06:27 PM
PROC Star
Posts: 8,163

## Re: Row to clumn

I can only guess at the variable lengths you might really need, but I think you are looking for something like:

data have;

informat attr \$80.;

input id attr &;

cards;

1  555-555-5555

1  (555) 555-5555

1  80 Here Street, Somewhere

2  1-555-555-5555

3  Apt 1A

;

set have;

by id;

if _n_ = 1 then pattern = prxparse("/\d\d\d-\d{4}/");

if first.id then do;

call missing(email);

call missing(phone);

end;

if PRXMATCH(pattern,attr) then phone=catx(',',phone,attr);

else if index(attr,"@") then email=catx(',',email,attr);

if last.id then output;

run;

🔒 This topic is solved and locked.