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 (March 2001, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 12 Mar 2001 10:31:16 -0500
Reply-To:     Bob Burnham <robert.a.burnham@DARTMOUTH.EDU>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Bob Burnham <robert.a.burnham@DARTMOUTH.EDU>
Organization: Dartmouth College
Subject:      Re: Importing a . file dat

Hi Richard,

I'm not so sure that this is such a simple problem -- if it was just that the data was spread over two lines, then that would be no trouble. On the other hand, if you always knew that the variable at the end of the first line would wrap, then that would be easy to handle. But I'm not sure there is any 'simple' way when you don't know whether a variable has wrapped from one line to the next consistently. (Of course, if the SAS gurus know of such a way then it would be fantastic if they could chime in.)

I can see a couple of different ways to handle it -- one would be to look at the input line as a string and check to see if the last character is a comma, which would mean that the field was complete. Then you could parse out the available variables and read the next line. This sounds like a lot of work to me (wow, am I lazy) when the alternative is just to simply re-write the file so that each observation is one one line. For example, if I have a file called "names.txt" that looks like this:

Harry, Potter, 4 Privet Drive George, Bush, 1400 Pennsylvania Avenue

I can just read in each line of the file and then write it out to a temporary location -- adding a CR only at the end of every other line.

%let LINES = 2; data _null_; infile "names.txt" truncover; file "buffer.txt"; length buffer $200; input buffer $200.; put buffer @; if(not mod(_N_,&LINES)) then put; run; data addr; infile "buffer.txt" delimiter=',' dsd; length fname lname street $80; input fname $ lname $ street $; run;

Of course, you'll need to think about a couple of other things when you actually do this -- the length of the each record in your file might mean that you would have to change LRECLs and the width of the variable that you are using for input. All in all though, I think this might be the most straightforward approach. . .

If you want to be really lazy (and impatient and full of hubris) then you can reformat your file with Perl before reading it with SAS:

while(<STDIN>) { chomp; if ($. % 2) { print "$_ "; } else { print "$_\n"; } }

Hope this helps (but there is probably a better way!!),

Bob

-- Bob Burnham robert.a.burnham@dartmouth.edu http://www.dartmouth.edu/~bburnham

"richard Simhon" <Richard.Simhon@CORNHILL.CO.UK> wrote in message news:2710430F08DAD211A3F70008C71EEE6904333D01@s-cho-exch0001... > All, > > This appears to be a simple problem but it's Monday!! > > I am importing a .dat file into SAS which uses a comma as a separator. > Each record goes across 2 lines. A variable may not finish exactly at the > end of the first line and may therefore be split onto the second line. > > Is there a way in the infile statement or elsewhere in the import statement > that I can get SAS to go to the second line and read the data for that > record. > > Thanks in advance > > Richard Simhon > Business Analyst > Allianz Cornhill > Tel +(44) 01483 552628


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