Help using Base SAS procedures

Comma delmitied variable to multiple rows

Accepted Solution Solved
Reply
Super Contributor
Posts: 400
Accepted Solution

Comma delmitied variable to multiple rows

Hello,

I have a csv file that I'm working on that list members to a club.  The data is very bad and I'm trying to clean it up.  The first issue is that they put multiple people to one row using commas to seperate.  So the fname field could have 1 or many commas.  I want to take every field and leave it the same except for FName.  For example

FName                           LName       Address        

Bob, Joe, William, Dave  Jones         1234 Test St

There are many more variables but this is how I need the data to render

FName                           LName       Address        

Bob                               Jones         1234 Test St

Joe                               Jones         1234 Test St

William                         Jones          1234 Test St
Dave                            Jones         1234 Test St

Thank you for any help you can give me.


Accepted Solutions
Solution
‎03-03-2014 04:57 PM
PROC Star
Posts: 7,416

Re: Comma delmitied variable to multiple rows

data have;

  informat FName LName Address $50.;

  input (FName  LName       Address) (&);

  cards;       

Bob, Joe, William, Dave  Jones         1234 Test St

;

data want (drop=_Smiley Happy;

  length FName $50;

  set have (rename=FName=_FName);

  _i=1;

  do while (scan(_FName,_i,',') ne '');

    FName=scan(_FName,_i,',');

    output;

    _i+1;

  end;

run;

View solution in original post


All Replies
Solution
‎03-03-2014 04:57 PM
PROC Star
Posts: 7,416

Re: Comma delmitied variable to multiple rows

data have;

  informat FName LName Address $50.;

  input (FName  LName       Address) (&);

  cards;       

Bob, Joe, William, Dave  Jones         1234 Test St

;

data want (drop=_Smiley Happy;

  length FName $50;

  set have (rename=FName=_FName);

  _i=1;

  do while (scan(_FName,_i,',') ne '');

    FName=scan(_FName,_i,',');

    output;

    _i+1;

  end;

run;

Super Contributor
Posts: 400

Re: Comma delmitied variable to multiple rows

Hi Arthur,


Thank you so much.  That worked perfectly.

You have been a great help to me and I truly appreciate it.

Thank you,

Jerry

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 191 views
  • 0 likes
  • 2 in conversation