DATA Step, Macro, Functions and more

How to Change Char variable into Date format.

Accepted Solution Solved
Reply
Super Contributor
Posts: 276
Accepted Solution

How to Change Char variable into Date format.

Hi..

Happy New year to all SAS People..

I had a question about variable conversion from char to date..

I am having a dataset and DOB is the one of the variable in that dataset.

the value in that variable is like 20120131,20120215(YYYYMMDD).

Now i want to convert this variable into date format..

i am using bellow logic and it working perfectly..

Data  test;

input date $20. ;

cards;

20120102

20120108

20120110

20120131

;

run;

data y;

set test;

Format date1 date9.;

date=strip(substr(date,5,2)||'/'||substr(date,7,2)||'/'||substr(date,1,4));

date1 =input(substr(strip(date),1,10),MMDDYY10.);

run;

PRoc print;

run;


.but my strong believe is i am doing bit tricky ,we do this in more simple way by using formats and informats..

Could any body suggest me how to accomplish this task in a simple way???



Thanks in Advance..


Regards..


Sanjeev.K


Accepted Solutions
Solution
‎01-04-2013 09:24 AM
Trusted Advisor
Posts: 1,137

Re: How to Change Char variable into Date format.

Posted in reply to kuridisanjeev

In that case sanjeev,

you may follow the below code

data xyz;

    set test;

    new_date=input(date,yymmdd8.);

    format new_date date9.;

run;

Thanks,

Jag

Thanks,
Jag

View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: How to Change Char variable into Date format.

Posted in reply to kuridisanjeev

Hi Sanjeev,

hope this is what you were looking for

Data  test;

input date yymmdd8. ;

format date date9.;

cards;

20120102

20120108

20120110

20120131

;

run;

Thanks,

Jag

Thanks,
Jag
Super Contributor
Posts: 276

Re: How to Change Char variable into Date format.

Posted in reply to Jagadishkatam

Hi Jag..

Actually i am getting data from client.so i ca not use dataline or cards in this situation.

Just for demonstration i used cards to create a dataset..

If i apply your solution i am getting bellow error..

data xyz;

set test;

informat date yymmdd8.;

format date Date9.;

run;

ERROR : Variable date has been defined as both character and numeric

Regards..

Sanjeev.K

Solution
‎01-04-2013 09:24 AM
Trusted Advisor
Posts: 1,137

Re: How to Change Char variable into Date format.

Posted in reply to kuridisanjeev

In that case sanjeev,

you may follow the below code

data xyz;

    set test;

    new_date=input(date,yymmdd8.);

    format new_date date9.;

run;

Thanks,

Jag

Thanks,
Jag
Contributor
Posts: 46

Re: How to Change Char variable into Date format.

Posted in reply to kuridisanjeev

Hi Sanjeev

Jag's suggestion is probably the simplest but if you needed to specify a format that isn't built in, We do this sort of task using Informats as you suggested.

the code blow will create an informat called MyDteFmt to cover all dates from 01Jan200 to 31Dec2020. you could put this in the autoexec for an EG project or even for the workspace server to make it available to all future processes.

     data infmt ;

          length fmtname $8 type $1 label 8 start $9;

          retain fmtname "MyDteFmt" ;

         do label = '01Jan2000'd to '31Dec2020'd;

               start = cat(put(year(label),z4.), put(month(label), z2.), put(day(label), z2.))  ;

               type = "I"; /* numeric InFormat */

               output ;

          end ;

     run ;

     proc format cntlin=infmt;

     run ;

     proc datasets lib=work memtype=data force nowarn nolist;

          delete infmt;

     run;

With this run your code would look something like this...

Data test;

     input date $20. ;

     cards;

20120102

20120108

20120110

20120131

;

run;

data y;

     set test;

     Format date1 date9.;

     date1 =input(strip(date),MyDteFmt.);

run;

Proc print;

run;

Hope this helps,

Tim.

Super Contributor
Posts: 276

Re: How to Change Char variable into Date format.

Posted in reply to kuridisanjeev

Thanks all for quick response ...

Rgards..

Sanjeev.K

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 3500 views
  • 3 likes
  • 3 in conversation