Scénario de test & Cas d'usage
Execution of SAS FCMP functions within the CAS environment.
Discover all actions of fcmpactCreate sensor data, including a record with a baseline of zero, which will trigger the bug in the initial function.
| 1 | DATA casuser.sensor_readings(promote=yes); |
| 2 | INFILE DATALINES dsd; |
| 3 | INPUT sensorId $ reading baseline; |
| 4 | DATALINES; |
| 5 | SEN001,105,100 |
| 6 | SEN002,50,0 |
| 7 | SEN003,220,200 |
| 8 | ; |
| 9 | RUN; |
| 1 | PROC CAS; |
| 2 | SESSION casauto; |
| 3 | fcmpact.addRoutines |
| 4 | routineCode={ 'function calculate_deviation(reading, baseline); |
| 5 | /* Buggy: does not check for baseline=0 */ |
| 6 | return ((reading - baseline) / baseline) * 100; |
| 7 | endsub;' }, |
| 8 | package='iot.analytics', |
| 9 | funcTable={name='iot_funcs', caslib='casuser', replace=true}; |
| 10 | RUN; |
| 1 | PROC CAS; |
| 2 | SESSION casauto; |
| 3 | DATA casuser.analysis_v1(promote=yes); |
| 4 | method RUN(); |
| 5 | SET casuser.sensor_readings; |
| 6 | deviation_pct = calculate_deviation(reading, baseline); |
| 7 | END; |
| 8 | dcl package fcmpact('iot.analytics') p; |
| 9 | RUN; |
| 10 | |
| 11 | TABLE.fetch / TABLE={caslib='casuser', name='analysis_v1'}; |
| 12 | RUN; |
| 1 | PROC CAS; |
| 2 | SESSION casauto; |
| 3 | fcmpact.addRoutines |
| 4 | routineCode={ 'function calculate_deviation(reading, baseline); |
| 5 | /* Fixed: handles baseline=0 */ |
| 6 | if baseline = 0 then return (0); |
| 7 | return ((reading - baseline) / baseline) * 100; |
| 8 | endsub;' }, |
| 9 | package='iot.analytics', |
| 10 | funcTable={name='iot_funcs', caslib='casuser', replace=true}; |
| 11 | RUN; |
| 1 | PROC CAS; |
| 2 | SESSION casauto; |
| 3 | DATA casuser.analysis_v2(promote=yes); |
| 4 | method RUN(); |
| 5 | SET casuser.sensor_readings; |
| 6 | deviation_pct = calculate_deviation(reading, baseline); |
| 7 | END; |
| 8 | dcl package fcmpact('iot.analytics') p; |
| 9 | RUN; |
| 10 | |
| 11 | TABLE.fetch / TABLE={caslib='casuser', name='analysis_v2'}; |
| 12 | RUN; |
The first analysis (analysis_v1) will show a missing value or error for SEN002 due to division by zero. The 'addRoutines' call in step 3 successfully overwrites the function in the 'iot_funcs' table. The second analysis (analysis_v2) completes without error for all records. The 'deviation_pct' for SEN002 is now 0, while the other sensors (SEN001, SEN003) show correct deviations of 5.0 and 10.0 respectively.