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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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