I'm trying to merge a date field from numeric year, day, and month to a character yyyymmdd to fit with another dataset. Originally when I ran the code it gave the length of the new field $600 for some reason, so I tried modifying the lengths myself. Now it's saying that the fields I'm trying to combine are defined as both numeric and character, and it won't combine them. I'm confused how this happened.
CBPdata is my input, date is my output. My original variables were Month, Day, and Year and I want to create the variable STADATE with yyyymmdd format.
Here is my code
data work.date;
format mon $char2.;
informat mon $char2.;
mon=cat(Month);
if Month=1 then mon='01';
if Month=2 then mon='02';
if Month=3 then mon='03';
if Month=12 then mon='12';
format d $char2.;
informat d $char2.;
d=cat(Day);
if day=1 then d='01';
if day=2 then d='02';
if day=3 then d='03';
if day=4 then d='04';
if day=5 then d='05';
if day=6 then d='06';
if day=7 then d='07';
if day=8 then d='08';
if day=9 then d='09';
format yr $char4.;
informat yr $char4.;
yr=cat(Year);
STADATE = yr||mon||d;
set work.date;
run;
Your data step looks wrong.
You have assignment statements before the SET statement.
You appear to be using FORMAT statements to define variables. You should be to use LENGTH or ATTRIB statement to define a varaible.
Also don't overwrite your input data, especially if you are not sure the code works.
data want;
set have ;
length new_char_date $8 ;
new_char_date = put( mdy(month,day,year), yymmddn8.);
run;
PPlease show us (a portion of) the data in both data sets.
Your data step looks wrong.
You have assignment statements before the SET statement.
You appear to be using FORMAT statements to define variables. You should be to use LENGTH or ATTRIB statement to define a varaible.
Also don't overwrite your input data, especially if you are not sure the code works.
data want;
set have ;
length new_char_date $8 ;
new_char_date = put( mdy(month,day,year), yymmddn8.);
run;
I've tried putting the SET statements where the should be (at the top), and then SAS keeps telling me I need to put LENGTH before SET in the Log. So I've already tried doing that.
I tried using LENGTH to change the length but it wouldn't let me do that either so I used FORMAT and that changed the length and format in the code
I was "overwriting" the input because for some reason, simple coding isn't working. Starting to wonder if there's something wrong with the input file.
I am going to see if your code works.
Thanks,
I'm not sure what was going on. I was working off of another code that I had used to do this before and for some reason it wasn't working this time. That's why I come here haha.
@jswinford wrote:
I've tried putting the SET statements where the should be (at the top), and then SAS keeps telling me I need to put LENGTH before SET in the Log. So I've already tried doing that.
I tried using LENGTH to change the length but it wouldn't let me do that either so I used FORMAT and that changed the length and format in the code
I was "overwriting" the input because for some reason, simple coding isn't working. Starting to wonder if there's something wrong with the input file.
I am going to see if your code works.
You cannot change the LENGTH of a character variable after SAS has already defined the variable. So if the source dataset already have a variable named CHAR_DATE then putting the LENGTH statement after the SET statement will generate a warning from SAS and be ignored. You can change the FORMAT attached to a variable, but that just changes how it is displayed and not how long it really is. So you could attach $600. format to variable that has length of $10 (and the reverse!).
Most likely the problems you had with using the LENGTH statement were related to using the same dataset name for the input and output of the data step and running the step more than once. So the first time the LENGTH statement defined a new varaible, but then when you ran it again reading the updated dataset it will ignore the LENGTH statement since the variable was already defined. You can use the DROP= dataset option to eliminate a variable that you want to recreate from other data.
Use the MDY() function, the following creates a SAS date called myDate and then a character date from it, called myDate_Char.
myDate = mdy(month, day, year);
format myDate yymmdd10.;
myDate_Char = put(myDate, yymmddn8.);
You also have your SET statement after your code. I suspect that might introduce some behaviour you're not expecting. I'd put it at the top of your code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.