LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (April 2006, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 13 Apr 2006 21:18:56 -0400
Reply-To:     PharmaLogic <pharmalogic.llc@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         PharmaLogic <pharmalogic.llc@GMAIL.COM>
Subject:      Re: transfer character variable to numeric variable( date)
Comments: To: Mindy <master2005_sas@yahoo.com>
In-Reply-To:  <d571e7e70604131738y18f3ffe1hbf09cdfc221055c3@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

A useful macro with example expanded upon you data: /* -------------------------------------------------------------------- */ /* Macro to convert a string date like 20030129175500 to a sas datetime */ /* where the string is in the format yyyyMnDyHrMiSe and length=14 */ /* -------------------------------------------------------------------- */ dm 'log' clear;

dm 'out' clear;

option notes spool ls=132 ps=60;

%macro conv_date(dsin,dsout,dt_var,dt_out,date_or_dt=DT,show=n,drop_partials=y);

%* *** Convert a string date to a sas datetime. *** *;;

/* ---------------Example String Date------------| | data a; | | input dtime $14.; | | datalines; | | 20030122185000 | | 20030925092316 | | ; | | run; | ----------------------------------------------- */

data &dsout;

set &dsin;

format date &dt_out._dp date9. &dt_out datetime20.;

date = input(substr(&dt_var,1,8), ?? yymmdd8.);

hr = input(put(input(substr(&dt_var,9,2), ?? 4.),z4.),4.); min = input(put(input(substr(&dt_var,11,2), ?? 4.),z4.),4.); sec = input(put(input(substr(&dt_var,13), ?? 4.),z4.),4.);

&dt_out = dhms(date,hr,min,sec);

&dt_out._dp=datepart(&dt_out);

%if %upcase(&drop_partials)=Y %then drop hr min sec;

run;

%if %upcase(&show)=Y %then %do; title Conversion of String dates;

proc print data=&dsout width=min double;

run;

title;

%end;

%mend conv_date;

data a;

length string_date $14.; patient='0001'; string_date='20050228'; output; patient='0002'; string_date='20050231'; output; patient='0003'; string_date='20050902175203'; output; run;

proc print data=a; run cancel;

%conv_date(a,b,string_date,realdate,date_or_dt=d,show=y,drop_partials=n);

dm 'out' top;

On 4/13/06, PharmaLogic <pharmalogic.llc@gmail.com> wrote: > > Well, it's not a date. There is no Feb 31st. On up to 29th on leap year. > > > On 4/13/06, Mindy <master2005_sas@yahoo.com> wrote: > > > > Hey, guys, > > > > I have a question regarding how to transfer a character variable to SAS > > date variable. > > > > The detail is I have a SAS data set TRY, which including a character > > variable A such as "20050231", how can I transfer A to numeric > > variable, which correspondences to Feb31, 2005. The reason of doing > > this transfer is that I need to calculate the difference of this > > variable with another date variable. > > > > I try to use realdate=input(c_date, yymmdd8.); , but get error message. > > > > Thanks a lot ! > > > > Mindy > > > > > > -- > PharmaLogic, LLC > 484.919.4925 > PharmaLogic.LLC@gmail.com >

-- PharmaLogic, LLC 484.919.4925 PharmaLogic.LLC@gmail.com


Back to: Top of message | Previous page | Main SAS-L page