BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
parmis
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data test;
    chardate='01-18';
    date=input(chardate, anydtdte5.);
    format date monyy7.;
run;

View solution in original post

13 REPLIES 13
novinosrin
Tourmaline | Level 20

what does : ex:01-18  value represent in terms on date/month/year?

 

 

parmis
Fluorite | Level 6

Month-year, It's January 2018

PeterClemmensen
Tourmaline | Level 20

So what does your character date look like? What date does 01-18 represent? The first of january 2018?

parmis
Fluorite | Level 6

month and Year, It's January 2018

novinosrin
Tourmaline | Level 20
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;
PeterClemmensen
Tourmaline | Level 20
data test;
    chardate='01-18';
    date=input(chardate, anydtdte5.);
    format date monyy7.;
run;
parmis
Fluorite | Level 6

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

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
parmis
Fluorite | Level 6

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;

PaigeMiller
Diamond | Level 26

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

 

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

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.

Tom
Super User Tom
Super User

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.);

 

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 13 replies
  • 1528 views
  • 0 likes
  • 6 in conversation