BookmarkSubscribeRSS Feed
jenim514
Pyrite | Level 9

Hi,

 

I have date character strings that I need to change to numeric dates.  The values are either YYYY or YYYY-MM

 

DATES

2016

2015

2015-11

2015-10

 

since they are different format types how can I change them to numeric?

 

This code leaves YYYY-MM - actually all values null.

 

if length(startc)=7 then startn=input(startc,yymmd.);

 

Help appreciated!!

4 REPLIES 4
Astounding
PROC Star

You have to realize that dates refer to a specific day in SAS.  Converting 2016 to a date, for example, might mean getting January 1, 2016 as the result.  Converting 2015-11 might mean getting November 1, 2015 as a result.  At any rate, this can be done easily with one statement:

 

data want;

set have;

datevar = input( trim(dates) || '-01-01', yymmdd10.);

run;

 

You would probably want to add a FORMAT statement, so that DATEVAR looks like a date when it prints.  Pick any format you would like, such as:

 

format datevar date9.;

format datevar yymmdd10.;

jenim514
Pyrite | Level 9

Isn't there a way to just keep 2016 as just a numeric year 2016 with out converting it to an actual date(with a month and day), and have the 2016-10 converted to a numeric date yymm date?  It doesn't have to be an actual date format I guess, just numeric.

 

 

Astounding
PROC Star

If you want to use just one variable to hold the numeric value, that variable will have a format so that it prints looking like a date.  There is a way (a complex way) to design a format that could print in 3 different forms depending on the size of the variable.  So you could define a format that would print numbers below 3000 in 4 digits, for example, but numbers above 3000 in a different form.  To do that, you would need to know something about the values that your dates take on, and whether there would be a conflict.  For example, dates in the 1960s get stored internally as integers below 3000 (mostly) and the format might not be possible.

 

Simpler would be to keep both variables.  Print the character version that you already have when that is appropriate, and recognize that the numeric dates represent a particular day.

ballardw
Super User

@jenim514 wrote:

Isn't there a way to just keep 2016 as just a numeric year 2016 with out converting it to an actual date(with a month and day), and have the 2016-10 converted to a numeric date yymm date?  It doesn't have to be an actual date format I guess, just numeric.

 

 


Do you want this numeric to sort properly? 2016 would come way before 201511 as a numeric. What about the values that have a complete date?

 

You should describe exactly how you intend to use this not-a-date field later to get better suggestions. Though if anything will involve grouping by year or quarter or month or determining intervals between values then a date value is likely the best way to go.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 696 views
  • 0 likes
  • 3 in conversation