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!
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.
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.
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
@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?
Yes of course haha
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."
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."
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.