Date: Sat, 17 Jan 2004 06:12:01 -0500
Reply-To: Peter Crawford <peter@CRAWFORDSOFTWARE.DEMON.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Peter Crawford <peter@CRAWFORDSOFTWARE.DEMON.CO.UK>
Subject: Re: How to read in a file with various delimiters?
on 11 January 2004,
Vlad <vlad.grechko@mail.ru> writes
>Hello All,
>
>can someone, please, help me to read in a file with SPACE and TAB
>delimeted values.
>
>The exampe of the file (the misaligned values could - or could not -
>be peresent and are quite the same as in the original file) is as
>follows:
>**** start of file ***;
>1
> 2
> 3
>10 11 12
> 20 21 22
>4
>5
> 100 101 102 103
>200 201 202 203
>6
>7
>8
>1000 1001 1002 1003 1004
> 2000 2001 2002 2003 2004
>**** EOF *****;
>
>Overall, there are a bit more than 100 variables, divided in portions
>of misaligned values (I used the values in tens, hundreds and
>thousands to make an example and distinguish between separate groups).
>
>The PROBLEM is that vars in "tens" could be separated by spaces, by
>the variables in "hundreds" could be separated by tabs, or the other
>way around.
>
>I was trying to read this file as follows:
>input (var1-var100) (:12.);
>
>If I don't use the DLM option, then my tab-delimited values are not
>read correctly. When I used DLM='09'X, then my space-delimited values
>are read as mising.
>
>I would _greatly_ appreciate your help!
>
>Thanks,
>Vlad
Sorry this response comes so late, and hope it is too late
I don't know whether Vlad's problem is delimiters
being the pair (blank and tab together) ..............1
or
are two alternative values .................2
or
changing at a predictable point in the input ........3
but here are some ideas for each
The trick with alternative 1, ........
data ;
infile 'your file' dlm = '09'x ....more infile options.... ;
input @ ; *open input buffer ;
* in the buffer, replace the pair blank+tab with just a tab,
before you parse it into variables with input statements;
_infile_ = tranwrd( _infile_, '2009'x /* blank followed by tab*/
, '09'x /* just a tab */
);
input ............... ;
** regular inputs resolved with just one tab;
run;
For alternative two, things are perhaps easier.
On your infile statement,
infile 'your file' dlm ='2009'x ....more infile options.... ;
That defines alternative delimiters tab(09x) and blank(20x)
If your problem is like the alternative 3, I described above, then
......... discover that the DLM= option of the infile statement can
refer to a variable, which is evaluated at the point where the infile
statement appears........ Consider
data ....;
if _n_ > 100 then mydlm = '09'x;
else mydlm = ' ' ;
infile 'your file' dlm = mydlm ....more infile options....;
input ................ ;
run;
That code resolves to use a tab delimiter after the first 100
data step iterations. You could use other criteria.
Good Luck
--
Peter Crawford
Crawford Software Consultancy
UK
|