Date: Wed, 11 Dec 2002 17:27:13 -0500
Reply-To: Howard_Schreier@ITA.DOC.GOV
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Howard_Schreier@ITA.DOC.GOV
Subject: Re: scan function question
Here's a maybe cheesy/sleazy way.
Use the DSD option of the INFILE statement.
Create a temporary one-line file to provide a buffer.
data _null_; file 'c:\temp\temp'; put; run;
There is probably a less kludgy way to do this.
NOw the DATA step can move the string values into the input buffer and use
INFILE and INPUT statement features.
data _null_;
length word1 $8;
length str $16;
infile 'c:\temp\temp' dsd dlm='-';
input @@;
str='1-5678';
_infile_ = str;
input @1 word1 $ @@;
put 'Entire String: ' str/ 'The First Word: ' word1;
str='-5678';
_infile_ = str;
input @1 word1 $ @@;
put 'Entire String: ' str/ 'The First Word: ' word1;
stop;
run;
Results:
Entire String: 1-5678
The First Word: 1
Entire String: -5678
The First Word:
NOTE: 1 record was read from the infile 'c:\temp\temp'.
The minimum record length was 0.
The maximum record length was 0.
On Wed, 11 Dec 2002 10:55:50 -0500, Primak, Philip
<Philip.Primak@GENZYME.COM> wrote:
>Hi SAS users,
>
>I have to select the first word of the string using dash as delimiter. As
>you can see from the LOG below, it does not work correctly when the string
>starts from dash. In that case it chooses the 2nd word instead of the 1st
>one. Is there a way to fix it? The only idea that has come to my head so
far
>is use trick like word1=scan(' '||str,1,'-') (Alternative way like check
the
>1st character using substr function I have rejected - it is not elegant and
>too strait forward). Any better ideas? Also, WHY scan function is not
>working correctly?
>
>Advance thanks
>Philip Primak
>Genzyme
>
>
>244 data _null_;
>245 length word1 $8;
>246 length str $16;
>247
>248 str='1-5678';
>249 word1=scan(str,1,'-');
>250 put 'Entire String: ' str/ 'The First Word: ' word1;
>251
>252 str='-5678';
>253 word1=scan(str,1,'-');
>254 put 'Entire String: ' str/ 'The First Word: ' word1;
>255
>256 run;
>
>Entire String: 1-5678
>The First Word: 1
>Entire String: -5678
>The First Word: 5678
>NOTE: DATA statement used:
> real time 0.00 seconds
> cpu time 0.00 seconds