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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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