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

Hello there,

 

The current date format is yyyy-mm-dd, and proc contents shows the formate is $10, i want to change it into numeric format, but the following codes don't work. could anyone help me to find the reason?

 

data temp01; set temp01;
     date1=input(date,$10.);
	 format date1 mmddyy8.;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@Songchan  At compile time, SAS creates and stores the metadata/descriptor portion of the variables. Therefore, at execution you will need a new variable to stored the converted values, i.e in your case the converted char  date to num sas date.  This essentially means a new assignment.

 

Therefore

   numeric_sas_Date=  input (char_date, yymmdd10.);

The above states, you want to create a numeric sas date by reading nonstandard character date using the appropriate informat which is yymmdd10. This is what Paigemiller demonstrated earlier. I chimed in only to help you understand. 

 

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@Songchan wrote:


 The current date format is yyyy-mm-dd,


Okay, stop right there! It is yyyy-mm-dd, so don't convert using $10.

 

Use the appropriate informat that recognizes it as a calendar date

 

date1=input(date,yymmdd10.);

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @Songchan The way to think through intuitively is to understand the proc contents report. Since your proc contents report says the date is a character date, it is not stored as a number. When we talk of character date to be converted to a numeric date aka SAS date, we are basically dealing with something known as non standard data which could be a mix of chars/nums etc.

 

Typically we read non standard data with tools known as informats. Informats will read the number of bytes of values in the variable specified in it, and convert the value to a valid number in your case and therefore the requirement is to appropriate with a compatible informat which is yymmdd10. as opposed to character format $10.  Should the informat fail to read the nonstandard data and convert appropriately, it triggers the automatic variable _ERROR_=1 and writes a note to the log and writes a missing value. Of course there are certain modifiers, that can used to suppress this behavior but that is out of scope for your rather straight forward requirement.

 

HTH & Regards!

 

input(date,yymmdd10.);

 

 


@Songchan wrote:

Hello there,

 

The current date format is yyyy-mm-dd, and proc contents shows the formate is $10, i want to change it into numeric format, but the following codes don't work. could anyone help me to find the reason?

 

data temp01; set temp01;
     date1=input(date,$10.);
	 format date1 mmddyy8.;
run;

 

Songchan
Calcite | Level 5

Thank you for replying me, do you mean i can write like the following:

      data temp01; set temp01;
            input (date, yymmdd10.);
            format date yymmddn8.;
       run;

 

It still doesn't work.

novinosrin
Tourmaline | Level 20

@Songchan  At compile time, SAS creates and stores the metadata/descriptor portion of the variables. Therefore, at execution you will need a new variable to stored the converted values, i.e in your case the converted char  date to num sas date.  This essentially means a new assignment.

 

Therefore

   numeric_sas_Date=  input (char_date, yymmdd10.);

The above states, you want to create a numeric sas date by reading nonstandard character date using the appropriate informat which is yymmdd10. This is what Paigemiller demonstrated earlier. I chimed in only to help you understand. 

 

 

Songchan
Calcite | Level 5

 Thank you so much! That works!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4332 views
  • 1 like
  • 3 in conversation