Hello,
I have a date variable in $5. format. I'm using the input function to convert it to monyy7. and I'm getting the following error message:
"Invalid argument to function INPUT at line..."
My current date values: ex:01-18
My code:
data need;
set have;
format new_date monyy7.;
new_date = input (old_date, $5.);
run;
Thanks for you help!
data test;
chardate='01-18';
date=input(chardate, anydtdte5.);
format date monyy7.;
run;
what does : ex:01-18 value represent in terms on date/month/year?
Month-year, It's January 2018
So what does your character date look like? What date does 01-18 represent? The first of january 2018?
month and Year, It's January 2018
I guess it's the 18th of January in any given year. Or January 1st, 1918.
data want;
date='01-18';
length _temp $4;
do _n_=2,1;
_temp=cats(_temp,scan(date,_n_,'-'));
end;
want=input(_temp,yymmn4.);
format want monyy7.;
drop _:;
run;
data test;
chardate='01-18';
date=input(chardate, anydtdte5.);
format date monyy7.;
run;
"01-18" was just an example to show the format of the date. I do have other dates starting Jan 2018 to July 2019 (it's a monthly data).
when I submit the code you posted all my dates change to Jan 2018.
@parmis wrote:
"01-18" was just an example to show the format of the date. I do have other dates starting Jan 2018 to July 2019 (it's a monthly data).
when I submit the code you posted all my dates change to Jan 2018.
Can you be specific and show us the exact code that results in "all my dates change to Jan 2018"? As far as I can tell, the code from @PeterClemmensen works perfectly.
The following code works for all "01-18" dates but other dates like "02-18", "03-18"... becomes null in the new data set.
data need;
set have;
current_date='01-18';
new_date=input(current_date, anydtdte5.);
format new_date monyy7.;
run;
@parmis wrote:
The following code works for all "01-18" dates but other dates like "02-18", "03-18"... becomes null in the new data set.
data need;
set have;
current_date='01-18';
new_date=input(current_date, anydtdte5.);
format new_date monyy7.;
run;
Well, if you have the assignment statement
current_date='01-18';
then you are telling SAS to make current_date to be 01-18 for every record in data set HAVE. So SAS is doing exactly what you told it to do. Is that what you want? Is there a variable named CURRENT_DATE in data set HAVE?
That's perhaps because you would have ran the below assignment statement too
chardate='01-18';
which you should not as your objective is to covert the value that you read from the input dataset, which I suppose you would have using a SET statement.
Warning: You are asking for trouble trying to store years in just two digits.
Your INPUT statement doesn't make any sense. You are telling INPUT() to use the $ informat which means it generates a character value.
To generate a DATE value use an INFORMAT that generates a date value. It might be easiest given your MM-YY strings to just prefix it with '01-' so it looks like DD-MM-YY then you can use the DDMMYY informat to read that string into a DATE value.
new_date = input ('01-'||old_date, ddmmyy10.);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.