BookmarkSubscribeRSS Feed
Owen
Calcite | Level 5

Im trying to replace case statements with custom created proc fcmp functions within a proc sql statement. I've created the functions for translating

character strings (db2 timestamps) into SAS datetime values. These functions do their job, but they take alot more cpu time compaired to the case

statements. With 100k records it takes 70% more time, with 1m records its even worse: 128% more time.

The reason for wanting to use functions in the first place is that we hoped to get some performace increasement. Besides that, they take alot less typing

and are easier to maintain.

I hope to get some answers on the following questions:
- Can I optimize my custom functions so that they will perform better?
- Are functions faster, as fast as or slower compaired to case statements?

Below are our current case statement and the custom function. As you can see ive changed some format options because they are easier to read and use.

Case statement:
case
  when missing( DB2_DATETIME )
  then .
    
  when substr(left( DB2_DATETIME ), 1, 4) < "1582"
  then "01JAN1582:00:00:00.000000"dt
    
  when substr(left(DB2_DATETIME ), 1, 4) < "1926"
    or substr(left(DB2_DATETIME ), 1, 4) > "2095"
  then dhms(input(substr( DB2_DATETIME , 1,10), yymmdd10.), 0,0, input(substr( DB2_DATETIME , 12, 8), time8.))
    
  else dhms(input(substr( DB2_DATETIME , 1,10), yymmdd10.), 0,0, input(substr( DB2_DATETIME , 12, 15), time15.6))
end

Function:
function db2_to_sas_datetime(db2_datetime $);
  length sas_datetime 8
         year $4;

  year = substr(db2_datetime, 1, 4);
  
  if year < "1582"
  then
    sas_datetime = "01JAN1582:00:00:00.000000"dt ;
  else do;
    if "1926" < year < "2096"
    then
      sas_datetime = input(db2_datetime, ymddttm26.);
    else
      sas_datetime = input(substr(db2_datetime, 1, 19), ymddttm19.);
  end;

  return(sas_datetime);
endsub;

Im running these jobs in a SAS 9.3 environment.

4 REPLIES 4
SASKiwi
PROC Star

Just a thought but have you tried running your function in a DATA step and if so do you get a similar performance drop?

The reason I ask this is that SAS would have to translate your function into SQL if it is run in the SQL procedure, But if you run it in a DATA step this translation would not be required.

ChrisNZ
Tourmaline | Level 20

2 answers:

1) proc fcmp does add an overhead

2) you are comparing very different syntaxes

This is how you can see it:

* The function runs in 6s;

data _null_;

  do i=1 to 1e7;x=db2_to_sas_datetime(sas_datetime);end;

run;

*  The select statement runs in 0.1s;

data _null_;

do i=1 to 1e7;

select;

  when (missing( DB2_DATETIME )) t=.;

  when (substr(left( DB2_DATETIME ), 1, 4) < "1582")

    t= "01JAN1582:00:00:00.000000"dt;

  when (substr(left(DB2_DATETIME ), 1, 4) < "1926"

    or substr(left(DB2_DATETIME ), 1, 4) > "2095")

    t= dhms(input(substr( DB2_DATETIME , 1,10), yymmdd10.), 0,0, input(substr( DB2_DATETIME , 12, 8), time8.));

  otherwise t=dhms(input(substr( DB2_DATETIME , 1,10), yymmdd10.), 0,0, input(substr( DB2_DATETIME , 12, 15), time15.6));

end;

end;

run;

* The if test from the function runs in 1.6s on its own;

data _null_; 

do i=1 to 1e7;

  year = substr(db2_datetime, 1, 4);

  if year < "1582"

  then

    sas_datetime = "01JAN1582:00:00:00.000000"dt ;

  else do;

    if "1926" < year < "2096"

    then

      sas_datetime = input(db2_datetime, ymddttm26.);

    else

      sas_datetime = input(substr(db2_datetime, 1, 19), ymddttm19.);

  end;

end;

run;

ChrisNZ
Tourmaline | Level 20

If you want to keep using the function for its practicality, you should optimise its code.

LinusH
Tourmaline | Level 20

Just a thought, do you really have datetime data from before 1582 or after 2095, and consider them as correct?

Are those stored in DB2 as char? If so, you could just throw the input function using implicit pass-thru, chances are that SAS/ACCESS will convert it to DB2 SQL, hence improving performance.

Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1268 views
  • 0 likes
  • 4 in conversation