DATA Step, Macro, Functions and more

change date character string

Reply
Regular Contributor
Posts: 180

change date character string

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!!

Super User
Posts: 6,526

Re: change date character string

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

Regular Contributor
Posts: 180

Re: change date character string

[ Edited ]
Posted in reply to Astounding

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.

 

 

Super User
Posts: 6,526

Re: change date character string

[ Edited ]

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.

Super User
Posts: 12,994

Re: change date character string


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.

 

 

Ask a Question
Discussion stats
  • 4 replies
  • 65 views
  • 0 likes
  • 3 in conversation