In a data step to suppress in-formating issues we use '??' like bellow
data _null_;
x='31FEB2020'; /*Invalid date*/
put x=;
y=input(x,??date9.); /*example*/
put y=;
run;
Same thing when i try to implement using proc fcmp i get error. I have added the working code, please add '??' before the date formats to see the error for yourself.
proc fcmp outlib=work.funcs.trial;
function create_num_date(char_date $, type $);
if upcase(strip(type))='START' then do;
DAY=IFC(COMPRESS(SCAN(char_date,1))='UN','01',COMPRESS(SCAN(char_date,1)));
MON=IFC(COMPRESS(SCAN(char_date,2))='UNK','JAN',COMPRESS(SCAN(char_date,2)));
YEAR=COMPRESS(SCAN(char_date,3));
num_date=INPUT(COMPRESS(CATX(' ',DAY,MON,YEAR)),DATE9.); /*here*/
end;
else if upcase(strip(type))='END' then do;
DAY=IFC(COMPRESS(SCAN(char_date,1))='UN','UN',COMPRESS(SCAN(char_date,1)));
MON=IFC(COMPRESS(SCAN(char_date,2))='UNK','DEC',COMPRESS(SCAN(char_date,2)));
YEAR=COMPRESS(SCAN(char_date,3));
if DAY='UN' then num_date=INTNX("MONTH",INPUT(COMPRESS(CATX(' ',MON,YEAR)),MONYY7.),0,"END"); /*here*/
else num_date=INPUT(COMPRESS(CATX(' ',DAY,MON,YEAR)),DATE9.); /*here*/
end;
return(num_date);
endsub;
run;
options cmplib=work.funcs;
/*test data*/
data _null_;
format y date9.;
x='UN FEB 2020';
put x=;
y=create_num_date(x,'start');
put y=;
y=create_num_date(x,'end');
put y=;
x='UN UNK 2020';
put x=;
y=create_num_date(x,'start');
put y=;
y=create_num_date(x,'end');
put y=;
x='15 UNK 2020';
put x=;
y=create_num_date(x,'start');
put y=;
y=create_num_date(x,'end');
put y=;
x='15 JAN 2020';
put x=;
y=create_num_date(x,'start');
put y=;
y=create_num_date(x,'end');
put y=;
x='31 FEB 2020'; /*Invalid date*/
put x=;
y=create_num_date(x,'start');
put y=;
y=create_num_date(x,'end');
put y=;
run;
What I am trying to achieve is a no error from the second data step using the user created function create_num_date().
The only way I know it can be avoided is by using a "??" before the informat. How ever when I use the "??" in informat inside the procedure, I am getting errors. Requesting to suggest a better way or any way around this.
When you do include the ?? in your PROC FCMP, note that your ERROR states that it is expecting a format name and not an informat name. Also, note that when you do not include the ??, the DATA step that tests out your function indicates there is an error in the INPUTN function. This is a little confusing, because you used INPUT and INPUTN would require an informat. However, the INPUTN function does not support using the ? or ?? prefixes in an informat. You can check this by running the code below, which is a modified version of your first step.
data _null_;
x='31FEB2020'; /*Invalid date*/
put x=;
y=inputn(x,??date9.); /*example*/
put y=;
run;
So, it appears as though PROC FCMP is using INPUTN even though you used INPUT, which is causing the unexpected syntax error.
Possibly a silly question but why are you using compress(catx in
COMPRESS(CATX(' ',DAY,MON,YEAR))
When I think you get the same result with
CATS(DAY,MON,YEAR)
Insert a character with the Catx with the intent to immediately remove it with Compress looks odd to me.
You probably don't need or want the function, but if you go ahead just have the function convert the string. Then have the user pass the converted string to INPUT.
y=input(create_num_date(x,'end'),??date9.);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.