Learning SAS? Welcome to the exclusive online community for all SAS learners.

How to convert Numeric value to Date on SAS University

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

How to convert Numeric value to Date on SAS University

Hi all,

I am a newbie in SAS community. I really need your help to convert a value from Numeric format (BEST12) to Date format (YYMMDD.10) in SAS University Edition.
Please write the codes as easy to understand as possible. Many SAS experts usually write codes in a difficult way- they use abbreviations and a gergon I really find difficult to understand-. 


I have a dataset I called "DATASET_LISBETH" that I imported in SAS from a sas7bdat file. I used the following code to do that:

 

data DATASET_LISBETH;
set 'My_folder_path';
run;



Now I have displayed in the RESULT section 170 columns and millions of rows. Among these variables I have a variable called "Month" that should be read as a date. Unfortunately, SAS reads it as a numeric value. The variable is displayed in the following format:


201403
201404
201405
201406

And so on for million of  rows! 

Now SAS reads this variable as being Numeric - Format BEST12 -. And I need SAS to read and display it as a date - YYMMDD.10 Format -.

Any suggestion?

Thank you in advance!


Accepted Solutions
Solution
‎06-24-2017 10:44 AM
Super User
Posts: 5,099

Re: How to convert Numeric value to Date on SAS University

The first thing to understand is that these numbers are not dates.  These numbers contain year and month only.  A date refers to a specific day as well.  So you have to decide, for example, whether you would like your variable to represent the first day of the month (versus some other day of the month).

 

Assuming you do want the first day of the month, here are some lines of code you could add to your DATA step to change the value of the existing variable.

 

temp_string = put(month * 100 + 1, 8.);

month = input(temp_string, yymmdd8.);

format month yymmdd10.;

drop temp_string;

 

The first line creates a character variable holding the year month and day.  The second statement reads in that character string and converts it to the proper numeric value to represent the date.  The third statement assigns the format to use when displaying the date.  You could pick the format of your choice ... any of these would work:

 

format month date9.;

format month mmddyy10.;

format month yymmdds10.;

 

The last statement gets rid of the character variable that is no longer needed once the proper date value has been assigned.  There are other ways to calculate this result, but I don't think any of them will be simpler.

View solution in original post


All Replies
Solution
‎06-24-2017 10:44 AM
Super User
Posts: 5,099

Re: How to convert Numeric value to Date on SAS University

The first thing to understand is that these numbers are not dates.  These numbers contain year and month only.  A date refers to a specific day as well.  So you have to decide, for example, whether you would like your variable to represent the first day of the month (versus some other day of the month).

 

Assuming you do want the first day of the month, here are some lines of code you could add to your DATA step to change the value of the existing variable.

 

temp_string = put(month * 100 + 1, 8.);

month = input(temp_string, yymmdd8.);

format month yymmdd10.;

drop temp_string;

 

The first line creates a character variable holding the year month and day.  The second statement reads in that character string and converts it to the proper numeric value to represent the date.  The third statement assigns the format to use when displaying the date.  You could pick the format of your choice ... any of these would work:

 

format month date9.;

format month mmddyy10.;

format month yymmdds10.;

 

The last statement gets rid of the character variable that is no longer needed once the proper date value has been assigned.  There are other ways to calculate this result, but I don't think any of them will be simpler.

Contributor
Posts: 25

Re: How to convert Numeric value to Date on SAS University

@Astounding Thank you so much for your help and solution! 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 304 views
  • 1 like
  • 2 in conversation