Date: Mon, 4 Mar 2002 14:54:30 -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: help for Substr
The PUT function converts numeric values to character via a format. Its
implicit usage is the crux of the original issue (see below).
The Zw. format zero fills the result. Zero fill is not necessary here unless
there is data from the tenth century or earlier. TRIM eliminates trailing
blanks. It and LEFT (which eliminates leading blanks) are not needed, though
harmless, in this situation.
But I don't think anybody answered the original question as to why Hamani's
code did not work.
The code as written applied a character function (SUBSTR) to a numeric
expression (DATE). In that situation, SAS will convert using the BEST12.
format. In other words:
month=substr(date,5,2);
is implicitly
month=substr(put(date,best12.),5,2);
Since DATE is an 8-digit integer, the result of the PUT function is four
blanks followed by the eight digits. The SUBSTR function then extracts the
5th and 6th characters, which are the first two digits.
Either of the following would correct the immediate problem:
month=substr(put(date,8.),5,2);
month=substr(left(date),5,2);
However, I agree with those who suggested using SAS dates and date functions
instead.
On Fri, 1 Mar 2002 11:12:24 -0500, Elmaache, Hamani
<Hamani.Elmaache@CCRA-ADRC.GC.CA> wrote:
>Hi there.
>
>Thank you. Is exactely what I want
>"
>Or, you could leave your input as numeric and then convert the numbers at
>substring time, i.e. year=substr(trim(left(put(date,z8.))),1,4);"
>
>Even I don't understand the functions: trim and put(date,z8.)?
>
>Barrere.
>
>
>-----Original Message-----
>From: Gregg P. Snell [mailto:gsnell@DATASAVANTCONSULTING.COM]
>Sent: February 28, 2002 5:14 PM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: help for Substr
>
>
>Greetings,
>
>Reading the records as character data is definitely one way to do it. Of
>course, you will need to correct your first substr statement to include the
>proper beginning position, i.e. year=substr(date,1,4);
>
>Or, you could leave your input as numeric and then convert the numbers at
>substring time, i.e. year=substr(trim(left(put(date,z8.))),1,4);
>
>Or, if you are reading the input as numeric, why not read them as SAS date
>values, i.e. input date yymmdd8.; and then use other date functions, i.e.
>year=trim(left(put(year(date),z4.)));
>
>Of course, you could just stick with character data and read it as:
>input year $1-4 month $5-6 day $7-8;
>
>Or...there are probably another dozen solutions...
>
>
>Gregg P. Snell
>Data Savant Consulting
>http://www.datasavantconsulting.com
>(913) 638-4640
>(208) 977-1943 fax
>
>
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU]On Behalf Of
>Frank Schiffel
>Sent: Thursday, February 28, 2002 3:57 PM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: help for Substr
>
>
>read it in as character. then do the substring.
>
>substring works only on character data.
>
>try input date $8.
>
>>>> "Elmaache, Hamani" <Hamani.Elmaache@CCRA-ADRC.GC.CA> 2/28/02 3:56:57 PM
>>>>
>Hi there.
>I wante some help.
>I have data:
>
>data date;
>input date;
>cards;
>19470114
>19590814
>19421128
>19451228
>19431006
>19401012
>19351221
>19421216
>19400322
>19411016
>;
>
>/* I wante separate it like this :
>year month day
>
>1947 01 14
>1959 08 14
>1942 11 28
>1945 12 28
>1943 10 06
>1940 10 12
>1935 12 21
>1942 12 16
>1940 03 22
>1941 10 16
>*/
>
>/* I wrote the following code
>; but it doesn't work . Why ? */
>
>
> data year;
> set date;
> year=substr(date,4);
> month=substr(date,5,2);
> day=substr(date,7,2);
> *put @1 year @15 month @25 day;
> run;
>
> /* But juste I got this: */
> proc print;
> run;
>/* I wante separate it like this :
>year month day
>
>1947 01 14
>1959 08 14
>1942 11 28
>1945 12 28
>1943 10 06
>1940 10 12
>1935 12 21
>1942 12 16
>1940 03 22
>1941 10 16
>*/
>
>/* I wrote the following code
>; but it doesn't work . Why ? */
>
>
> data year;
> set date;
> year=substr(date,4);
> month=substr(date,5,2);
> day=substr(date,7,2);
> *put @1 year @15 month @25 day;
> run;
>
> /* I got this: */
>
> Obs date year month day
>
> 1 19470114 19470114 19 47
> 2 19590814 19590814 19 59
> 3 19421128 19421128 19 42
> 4 19451228 19451228 19 45
> 5 19431006 19431006 19 43
> 6 19401012 19401012 19 40
> 7 19351221 19351221 19 35
> 8 19421216 19421216 19 42
> 9 19400322 19400322 19 40
> 10 19411016 19411016 19 41
|