BookmarkSubscribeRSS Feed
turcay
Lapis Lazuli | Level 10

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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;

turcay
Lapis Lazuli | Level 10

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;

 

ballardw
Super User

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.

Shmuel
Garnet | Level 18

@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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1851 views
  • 0 likes
  • 3 in conversation