input(compress(put(datepart(),yymmdd10.),'-'),8.)
deriveDateDimPK(,”DATETIME”);
PROC FCMP OUTLIB=
PROC FCMP OUTLIB=sasuser.funcs.dmart;
function deriveDateDimPK (inputField, inputFormat $);
length dateDimKey 3
inDate 3;
if inputformat eq "DATETIME" then inDate = datepart(inputField);
else inDate = inputField;
return (input(compress(put(inDate,yymmdd10.),'-'),8.));
endsub;
QUIT;
OPTIONS CMPLIB=sasuser.funcs;
/* Create a table with date/time values */
DATA work.funcInput;
KEEP ID TRANSACTION_DTTM;
startDate = today();
do i = 0 to 9;
ID=i+1;
TRANSACTION_DTTM = dhms(startDate-i,0,0,0);
OUTPUT;
end;
RUN;
/* Show usage of deriveDateDim() function */
DATA _NULL_;
SET work.funcInput;
dateDimPK = deriveDateDimPK(TRANSACTION_DTTM,"DATETIME");
put ID= TRANSACTION_DTTM= datetime20. dateDimPK=;
RUN;
PROC SQL;
select ID,
TRANSACTION_DTTM,
deriveDateDimPK(TRANSACTION_DTTM,"DATETIME")
from work.funcInput;
QUIT;
Nice introduction to the benefits of user functions.
Just a note that often the SAS macro language can provide similar functionality.
So in this case, if your desired code was:
input(compress(put(datepart(<date/time field>),yymmdd10.),'-'),8.)
Your macro could be:
%macro makedate(field);
input(compress(put(datepart(&field),yymmdd10.),'-'),8.)
%mend makedate;
Used like:
DATA _null_;
SET work.funcInput;
dateDimPK = %makedate(TRANSACTION_DTTM);
put ID= TRANSACTION_DTTM= datetime20. dateDimPK=;
RUN;
Not a criticism of your post. Always more than one way to skin a cat.
Hi Tim
Thanks for the valuable information.
Could you please elaborate a bit more section "PROMOTING FUNCTIONS BETWEEN ENVIRONMENTS"? (the link there seems incomplete)
What confuses me:
"Remember from above that functions are stored in SAS datasets." I would have thought these are compiled entries in a SAS Catalog
"That being the case, they may be promoted as a Library and Table along with the rest of your DI Studio objects in a SAS package file..." But these packages are only metadata. The compiled functions as such won't be moved this way. Also as this is a catalog and not a table there won't be table metadata.
I was thinking so far that I would need some DI once-off job with the PROC FCMP code, that this code gets promoted and then needs to be re-run in the target environment, and if not compiling into the default location one would also need to amend the global option cmplib (eg. in the autoexec of the target environment).
.... but may be I'm missing something here. Could you please give more detail?
Thanks
Patrick
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.