BookmarkSubscribeRSS Feed
Jack_Smitherson
Fluorite | Level 6

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!

8 REPLIES 8
Reeza
Super User

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. 

 

 

Astounding
PROC Star

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.

Jack_Smitherson
Fluorite | Level 6

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

ballardw
Super User

@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?

Jack_Smitherson
Fluorite | Level 6

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

Reeza
Super User

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


 

ballardw
Super User

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

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
  • 8 replies
  • 857 views
  • 4 likes
  • 4 in conversation