BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tparvaiz
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
shivas
Pyrite | Level 9

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

3 REPLIES 3
shivas
Pyrite | Level 9

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

SteveNZ
Obsidian | Level 7

Try:

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

data want ;

    datetime = "&NEXT_DAY_1"dt ;

    date = datepart(datetime) ;

run ;

Alpay
Fluorite | Level 6

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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