DATA Step, Macro, Functions and more

How to - Convert Character&Numeric Variable to Date Format

Reply
Super Contributor
Posts: 395

How to - Convert Character&Numeric Variable to Date Format

Hello everyone,

 

I have a sample data set as below and I try to convert these values from Character&Numeric to Date9.

 

I tried to do something but it doesn't work it. Can somebody help me, please?

 

Data Have;
Length DateCharacter $ 10 DateNumeric 8 ;
Infile Datalines Missover;
Input DateCharacter DateNumeric ;
Datalines;
201001 201001
201002 201002
201003 201003
201004 201004
201005 201005
201006 201006
201007 201007
201008 201008
201009 201009
201010 201010
201011 201011
201012 201012
;
Run;

Data Want;
Set Have;
DateCharacterNew=Input(DateCharacter,Date9.);
DateNumericNew=Input(DateNumeric,Date9.);
Format DateCharacterNew Date9. DateNumericNew Date9.;
Run;

My desired output as below;

 

Desired.png

 

Thank you

Trusted Advisor
Posts: 1,586

Re: How to - Convert Character&Numeric Variable to Date Format

Your input dates are of the form: yyyymm.

 

To convert them you need attach a day (=01), thats it:

 

data have;

       infile datalines missover;

       input @1 datex1 $6.  @8 date_num  6. ;

 

       date1 = input(datex1||'01' , yymmdd8.);

       date2 = input(put(date_num * 100 + 1,z8.),yymmdd8.);

       format date1 date2 date9.;

datalines;

   .... your data ...

; run;

Super Contributor
Posts: 395

Re: How to - Convert Character&Numeric Variable to Date Format

Thank you,

If I put like as below, it is okay ;

 

 

data have;
       infile datalines missover;
       input @1 datex1 $6.  @8 date_num  6. ;
 
       date1 = input(datex1||'01' , yymmdd8.);
       date2 = input(put(date_num * 100 + 1,z8.),yymmdd8.);
       format date1 date2 date9.;
datalines;
201001 201001
201002 201002
201003 201003
201004 201004
201005 201005
201006 201006
201007 201007
201008 201008
201009 201009
201010 201010
201011 201011
201012 201012
;
run;

But if I write the code as below, date1 doesn't come  like which I  want. How can I solve it, can you help me?

 

Data Want;
Set Have;
date1 = input(DateCharacter||'01' , yymmdd8.);
date2 = input(put(DateNumeric * 100 + 1,z8.),yymmdd8.);
format date1 date2 date9.;
Run;

 

Super User
Posts: 11,343

Re: How to - Convert Character&Numeric Variable to Date Format

[ Edited ]

Where do the variables DATECHARCTER or DATENUMERIC get defined or assigned? The data set HAVE has the variable Datex1 which is character of length 6 and Date_num as a numeric variable, not the names you use in Want.

Trusted Advisor
Posts: 1,586

Re: How to - Convert Character&Numeric Variable to Date Format

@turcay if dataset HAVE was created as by

  

data have;
       infile datalines missover;
       input @1 datex1 $6.  @8 date_num  6. ;

then @ballardw is right and there are no  DateCharacter, DateNumeric variables in input.

but if you relate to your original code:

     

Data Have;
Length DateCharacter $ 10 DateNumeric 8 ;
Infile Datalines Missover;
Input DateCharacter DateNumeric ;

then the problem in date1 is because you defined length as $10 instead $6,

alternatively you can code:

  

date1 = input(trim(DateCharacter)||'01' , yymmdd8.);

which might work.

Ask a Question
Discussion stats
  • 4 replies
  • 263 views
  • 0 likes
  • 3 in conversation