Date: Mon, 12 Mar 2001 11:53:44 -0500
Reply-To: David Ward <dward@SASHELP.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: David Ward <dward@SASHELP.COM>
Subject: Re: Importing a . file dat
Content-Type: text/plain; charset="us-ascii"
Here is a one data step solution to reading in a file using dsd that spans two lines with an unknown break (could break in between any of the variables or none at all). If you know the number of variables in each record, you can use this technique:
In a nutshell, read the first line one field at a time (using dsd) until we get to the last field (by checking col and reclen). Before moving to the next line, get the last character in the first line, then get the first character in the second line. If both are not equal to ',' then assume the last field wraps. In this case, read the first field from the second line and concatenate it to the last field in the first line. Continue reading fields in the second line until we reach our field number.
You can use the select statement to set your real variables to the variable named temp (using any input() calls with appropriate formats if necessary to convert to numeric data).
Hope this helps
data _null_;
infile cards dsd length=reclen col=col truncover;
i=0; * Counter to keep track of which variable you are on;
do until(i>=5);
i=i+1;
input temp $ @; * Read in one varaible as a string;
if col>=reclen and not l then do;
* Check to see if this field wraps - read the last char from line 1
and the first char from line 2. If neither is a comma it wraps;
input @reclen t $1.; input u $1. @;
if t^=',' and u^=',' then do;
input @1 temp2 $ @;
temp=trim(temp)||temp2;
end;
l=1;
end;
select (i);
when (1) myvar=temp;
when (2) myvar2=temp;
* etc ;
otherwise;
end;
put i= temp=;
end;
put _all_;
cards;
one,two,thr
ee,four,five
;
run;
David Ward
|