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

Hi

how to move rows to columns:

id       attr

1       phone1

1      phone2

1     addr1

2     phone1

3     addr1

3    addr2

4   email1

id        phone                           addr                        email

1         phone1,phone2             addr1

2          phone1

3                                               addr1,addr2

4                                                                             email1

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

myname@notmyprovider.com

myname@myprovider.com

3  90 There Road, Elsewhere

3  Apt 1A

notmyname@notmyprovider.com

;

data want (keep=email address phone);

  set have;

  retain pattern address phone email;

  length address phone email $80;

  by id;

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

  if first.id then do;

    call missing(address);

    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);

  else address=catx(',',address,attr);

  if last.id then output;

run;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

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?

SASPhile
Quartz | Level 8

phone is typical phone numbers

addr is real address

and email has @

art297
Opal | Level 21

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

myname@notmyprovider.com

myname@myprovider.com

3  90 There Road, Elsewhere

3  Apt 1A

notmyname@notmyprovider.com

;

data want (keep=email address phone);

  set have;

  retain pattern address phone email;

  length address phone email $80;

  by id;

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

  if first.id then do;

    call missing(address);

    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);

  else address=catx(',',address,attr);

  if last.id then output;

run;

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
  • 3 replies
  • 753 views
  • 0 likes
  • 2 in conversation