3

I need to convert a character string (MM/YYYY) into a date (MM/YYYY). I was trying to use the format mmyyss. or mmyyss7. and received the following error log (same error if I use mmyys.). Any advise would be appreciated!:

data temp; 
set nv.nvPhaseI_II; 
format dtcd1 mmyys7.; 
                  -------
                  48
ERROR 48-59: The format $MMYYS was not found or could not be loaded.

38   run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEMP may be incomplete.  When this step was stopped there were 0
         observations and 525 variables.
WARNING: Data set WORK.TEMP was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.62 seconds
      cpu time            0.00 seconds
flag
What up KwonC?? Hey. Not to state the obvious, but I think the primary problem is that you're trying to apply a date format to a character variable. That's the source of the error. Should the variable be a date? or a character representation of a date? – jay.l.stevens Nov 13 at 12:51
The variable needs to be a date. Through the responses I realized where I went wrong. It's been a while since I've really written SAS code. Love this site. I have forwarded it to others!!! – KwonC Nov 13 at 15:42

3 Answers

3

The problem as stated has to do with what the FORMAT statement does and does not do.

1) SAS dates are numerics--the number of days since Jan 1, 1960.

2) The FORMAT statement doesn't transform anything; it merely associates a format with a variable so that, when you print or otherwise display it, it looks a certain way. Formats can be used within functions or PUT statements in order to create new character variables, but that's not what you did.

3) The error message is saying: you wanted me to associate a format with a character variable. That requires a character format, i.e., one that begins with a $. I (SAS) don't know about any format called $MMYYS.

4) To convert a character variable to a numeric variable, you need an informat--something that represents or transforms formatted character information. The output need not be a numeric, as you'll see when you look at the list of available informats. Many of them begin with a $ and thus have character strings as output.

5) SAS is consistent in its syntax: INPUT statement and INPUT function use informats, whereas PUT statement and PUT function use formats. Moreover, the PUT statement will write to a place specified by a FILE statement and the INPUT statement will get information from an INFILE-specified source.

6) As it happens, there is no MMYYS (or other MMYY*) informat.

7) Since you already have the character variable within a SAS data set, you just need to transform it using the INPUT function. There is a DDMMYYn informat--one that can handle various separators such as slash, dash and period, or none at all--and so Toloc's answer is correct.

8) Note that Toloc also attached a format to the converted date, thus making the value appear as mm/yyyy when you print it out. This has nothing to do with what happens in the statement that creates the converted date--he could have used any other date format.

9) If you want to represent the converted date differently somewhere in your output, you can override the FORMAT statement's association temporarily. For instance:

proc print data=temp2;
var convert_date;
format convert_date yymon5.;
run;

The result will be dates in the form 77NOV, etc.


data _null_;
file 'c:\stuff\nonsense.txt';
set temp2;
put convert_date date7.;
run;

Here you'll get a flat (not SAS) file containing dates in the form 01NOV77.

link|flag
3

I tried the first proposed solution but I don't get the right dates (eg: 11/1977 -> 11/2019) ?

You can do that with the following:

DATA temp2;

format convert_date mmyys7.;

SET temp; 

convert_date=INPUT(COMPRESS("01/"||chardate),DDMMYY10.);

RUN;
link|flag
The problem is caused by the 2nd input function using the format "2.". Use "4."... input(scan(chardate, 2, '/'), 4.) – SBloom Nov 12 at 15:00
Steve is correct. I should have used 4. Sorry for the confusion. – MarkLamias Nov 12 at 18:48
2

I would just use the MDY function and use the first of the month to create a proper date out of your character month and year and then format the date as mmyys7. as shown in the sample below:

data work.temp;
input chardate $7.;
cards;
11/1977
10/2006
;
run;

data work.temp2;
set work.temp;
format convert_date mmyys7.;
convert_date=mdy(input(scan(chardate, 1, '/'), 2.), 1, input(scan(chardate, 2, '/'), 2.));
run;
link|flag

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.