DATA Step, Macro, Functions and more

Dropping day and month from date (character variable)

Reply
Occasional Contributor
Posts: 16

Dropping day and month from date (character variable)

Hey everyone, 

 

I have a character variable on a SAS profile that has the year, month and day in the following format (example): 20120921.

 

I am trying to drop the month and day, in order to go from 20120921 to 2012. Could you please help me figure out the code I'd need to do that on SAS? 

 

Thanks in advance!

Super User
Posts: 23,992

Re: Dropping day and month from date (character variable)

Posted in reply to Jack_Smitherson

If it's a character use the SUBSTR() function to retrieve the first 4 characters.

 

I would recommend storing your date as a SAS date then you could use the YEAR function. 

 

 

Super User
Posts: 6,928

Re: Dropping day and month from date (character variable)

Posted in reply to Jack_Smitherson

If you're sure this is the right thing to do, here's a really easy way:

 

data want;

length my_date_var $ 4;

set have;

run;

 

Since the variable now only contains 4 characters, there's only room to store the first four characters.

Occasional Contributor
Posts: 16

Re: Dropping day and month from date (character variable)

Posted in reply to Astounding

Thanks for your response. Unfortunately, when I run that code, the character variable doesn't shrink down to 4 for some reason... I doubled checked on SAS and this variable in fact a character variable

Super User
Posts: 13,924

Re: Dropping day and month from date (character variable)

Posted in reply to Jack_Smitherson

Jack_Smitherson wrote:

Thanks for your response. Unfortunately, when I run that code, the character variable doesn't shrink down to 4 for some reason... I doubled checked on SAS and this variable in fact a character variable


Did you change the name of the variable to match your variable name?

Occasional Contributor
Posts: 16

Re: Dropping day and month from date (character variable)

Yes of course haha

Occasional Contributor
Posts: 16

Re: Dropping day and month from date (character variable)

When I run the code on SAS, this is the message that I get: 

"WARNING: Multiple lengths were specified for the variable VERDATE by input data set(s). This can
cause truncation of data."

Super User
Posts: 23,992

Re: Dropping day and month from date (character variable)

Posted in reply to Jack_Smitherson

Which method did you try? 

Psot your code and log. 


Jack_Smitherson wrote:

When I run the code on SAS, this is the message that I get: 

"WARNING: Multiple lengths were specified for the variable VERDATE by input data set(s). This can
cause truncation of data."


 

Super User
Posts: 13,924

Re: Dropping day and month from date (character variable)

Posted in reply to Jack_Smitherson

Jack_Smitherson wrote:

When I run the code on SAS, this is the message that I get: 

"WARNING: Multiple lengths were specified for the variable VERDATE by input data set(s). This can
cause truncation of data."


This tells us that you have multiple data sets and that the lengths of a variable are different in each. If this is the date variable you are manipulating then the description you started with may be inaccurate for some of the data due to truncation.

 

And it may be time to show the exact code you ran that did not work as needed.

Ask a Question
Discussion stats
  • 8 replies
  • 126 views
  • 4 likes
  • 4 in conversation