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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

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
kuridisanjeev
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16

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
TimCampbell
Quartz | Level 8

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.

kuridisanjeev
Quartz | Level 8

Thanks all for quick response ...

Rgards..

Sanjeev.K

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 9639 views
  • 3 likes
  • 3 in conversation