|
On 15 Jun, 07:52, Tree Frog <tree.fr...@hotmail.com> wrote:
> On Jun 15, 8:08 am, olan...@googlemail.com wrote:
>
> > Dear SAS Group,
>
> > I have a character variable which contains observations like this one
> > (11/7/2002 12:21:37) and wanted to create a date variable and two
> > separate date and time variables with no success. I would appreciate
> > any suggestion.
>
> > Thanks in advance
>
> Hi there
>
> What code have you tried? Why is it a character variable - can you re-
> import it from the source as a datetime using one of SAS' many
> INFORMATs?
>
> Assuming you can't re-import it, read up on grabbing substrings from
> character variables, and build the date and time variables from
> these. you may need to scan strings first for where the "/"s are to
> cater for 1- and 2-digit month values, and ensure your substrings are
> grabbing the right parts of the variable.
>
> This kind of task is good stuff - enjoy!
>
> TF
I'm not sure precisely what you want when you ask for 'a date variable
and two separate date and time variables' but the following might give
you a way forward.
I've stored the character string you described and split out the date
and time using SCAN. I've thenn input the date and time into sas date
and time values before 'put' ing the formatted date and time for you
to see. The look1 and look2 variables are there for you to see the
results of the SCAN.
data _null_;
cv = '11/7/2002 12:21:37';
look1 = scan(cv,1,' ') ;
look2 = scan(cv,2,' ') ;
date = input(scan(cv,1,' '),ddmmyy10.);
time = input(scan(cv,2,' '),time8.);
put look1= look2= date= time=;
format date date9. time time8.;
run;
If the code is run the following is seen in the SAS log.
look1=11/7/2002 look2=12:21:37 date=11JUL2002 time=12:21:37
NOTE: The data set WORK.A has 1 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Hope this is useful.
Regards,
Barry D.
|