converting character field to date field

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 129
Accepted Solution

converting character field to date field

Hi,

May I know how to convert characters to a date.. here is what I am doing

STEP 1.

I have a Macro that assigns date to NEXT_DAY_1, NEXT_DAY_2 and so on in the format 23Jun2012:00:00:00

STEP 2.

now I want to assign a date (using Macro) to a variable DAY_DATE using "if statement" such that if the day field = 'D1_VOL' then it should show the date stored in NEXT_DAY_1...

following code is working fine, except that the date field is stored as a character and not a date

DATA WORK.TABLE_A;

set WORK.TABLE_!;

if day = 'D1_VOL' then DAY_DATE = "&NEXT_DAY_1";

if day = 'D2_VOL' then DAY_DATE = "&NEXT_DAY_2";

Run;

Please advise how to convert DAY_DATE field into a date

Thanks in advance


Accepted Solutions
Solution
‎06-24-2012 06:49 AM
Super Contributor
Posts: 349

Re: converting character field to date field

Hi,

Try this...Hope it helps..

%let NEXT_DAY_1=23Jun2012:00:00:00;

%let NEXT_DAY_2=24Jun2012:00:00:00;

data sample;

input day $;

cards;

D1_VOL

D2_VOL

D3_VOL

;

run;

DATA WORK.TABLE_A;

set sample;

if day = 'D1_VOL' then DAY_DATE = input("&NEXT_DAY_1",datetime.);

if day = 'D2_VOL' then DAY_DATE = input("&NEXT_DAY_2",datetime.);

format day_date datetime.;

Run;

Thanks,

Shiva

View solution in original post


All Replies
Solution
‎06-24-2012 06:49 AM
Super Contributor
Posts: 349

Re: converting character field to date field

Hi,

Try this...Hope it helps..

%let NEXT_DAY_1=23Jun2012:00:00:00;

%let NEXT_DAY_2=24Jun2012:00:00:00;

data sample;

input day $;

cards;

D1_VOL

D2_VOL

D3_VOL

;

run;

DATA WORK.TABLE_A;

set sample;

if day = 'D1_VOL' then DAY_DATE = input("&NEXT_DAY_1",datetime.);

if day = 'D2_VOL' then DAY_DATE = input("&NEXT_DAY_2",datetime.);

format day_date datetime.;

Run;

Thanks,

Shiva

Frequent Contributor
Posts: 87

Re: converting character field to date field

Try:

%let NEXT_DAY_1 =  '23Jun2012:00:00:00' ;

data want ;

    datetime = "&NEXT_DAY_1"dt ;

    date = datepart(datetime) ;

run ;

Frequent Contributor
Posts: 95

Re: converting character field to date field

There is an exclamation mark, "!", in your data set name.

You can add "dt" after the closing quote to treat the value as a datetime value (number of seconds since 1/1/1960).

Use DATEPARTF() function to extract the date.

%let Next_Day_1 =  23Jun2012:00:00:00;

%let Next_Day_2 =  25Jun2012:00:00:00;

DATA WORK.TABLE_A;

set WORK.TABLE;

if day = 'D1_VOL' then DAY_DATE = DATEPART("&NEXT_DAY_1"dt);

else if day = 'D2_VOL' then DAY_DATE = DATEPART("&NEXT_DAY_2"dt);

%* The following should work, too;

/*

if day = 'D1_VOL' then DAY_DATE = "%SUBSTR(&NEXT_DAY_1,1,9)"d;

else if day = 'D2_VOL' then DAY_DATE = "%SUBSTR(&NEXT_DAY_2,1,9)"d;

*/

format Day_Date date9.;

Run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 412 views
  • 1 like
  • 4 in conversation