LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2001, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Tue, 2 Oct 2001 04:19:23 -0700
Reply-To:   Janis Jekabsons <Janis.Jekabsons@DRESDNER-BANK.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Janis Jekabsons <Janis.Jekabsons@DRESDNER-BANK.COM>
Organization:   http://groups.google.com/
Subject:   Reading a .csv with commas in fields
Content-Type:   text/plain; charset=ISO-8859-1

Hi all,

I am reading a comma-separated flat file. Some fields may contain a comma preceded by the \ escape character. So the data looks like this:

12345,One Way Drive 45\, 15,6789

which should be read like

ONE=12345 TWO=One Way Drive 45, 15 THREE=6789

To get around the problem I use the conditional input -- keeping the input line in buffer, then checking if the last character of TWO is a backslash and acting accordingly. So the program looks like this,

libname lib "/blabla";

filename da_d1 "/blabla/bla.txt";

data lib.da_d1; infile da_d1 dsd dlm=',' missover lrecl=590 recfm=v; input bla1 : $19. @ ; if substr(bla1,length(bla1),1)='\' then do; input bla1_ : $27. bla2 : $27. /* And now check bla2 also ... */ ; bla1 = left(trim(substr(bla1,1,length(bla1)-1)))||','||left(trim(bla1_)); end; else do; input bla2 : $27. /* And now check bla2 also ... */ ; end; run;

The problem is, of course, that, if this occurs in many fields, the program becomes unwieldy because of the nested do/else.

Maybe some kind soul out there knows of and can share a clue that can make this simpler? I am using SAS 8.1 on UNIX.

Regards,

Janis Jekabsons


Back to: Top of message | Previous page | Main SAS-L page