FCMP Procedure

Example 8: Standardizing Each Row of a Data Set

Features:
PROC FCMP functions:
RUN_MACRO
RUN_SASFILE
READ_ARRAY
WRITE_ARRAY
This example shows how to standardize each row of a data set.

Program

data numbers;
   drop i j;
   array a[5];
   do j = 1 to 5;
   do i = 1 to 5;
         a[i] = ranuni(12345) * (i+123.234);
      end;
      output;
      end;
   run;
%macro standardize;
%let dsname = %sysfunc(dequote(&dsname));
%let colname = %sysfunc(dequote(&colname));
proc standard data = &dsname mean = &MEAN std = &STD out=_out;
   var &colname;
run;
data &dsname;
   set _out;
run;
%mend standardize;
proc fcmp outlib = sasuser.ds.functions;
   subroutine standardize(x[*], mean, std);
      outargs x;

      rc = write_array('work._TMP_', x, 'x1');
      dsname = 'work._TMP_';
         colname = 'x1';
         rc = run_macro('standardize', dsname, colname, mean, std);
      array x2[1]_temporary_;
         rc = read_array('work._TMP_', x2);
      if dim(x2) = dim(x) then do;
         do i = 1 to  dim(x);
            x[i] = x2[i];
          end;
         end;
   endsub;
run;
options cmplib = (sasuser.ds);
data numbers2;
   set numbers;
   array a[5];
   array t[5]_temporary_;
   do i = 1 to 5;
      t[i] = a[i];
   end;
   call standardize(t, 0, 1);
   do i = 1 to 5;
      a[i] = t[i];
   end;
   output;
run;
proc print data=work.numbers2;
run;

Program Description

Create a data set that contains five rows of random numbers.
data numbers;
   drop i j;
   array a[5];
   do j = 1 to 5;
   do i = 1 to 5;
         a[i] = ranuni(12345) * (i+123.234);
      end;
      output;
      end;
   run;
Create a macro to standardize a data set with a given value for mean and std.
%macro standardize;
%let dsname = %sysfunc(dequote(&dsname));
%let colname = %sysfunc(dequote(&colname));
proc standard data = &dsname mean = &MEAN std = &STD out=_out;
   var &colname;
run;
data &dsname;
   set _out;
run;
%mend standardize;
Use the FCMP function to call WRITE_ARRAY, which writes the data to a data set. Call RUN_MACRO to standardize the data in the data set. Call WRITE_ARRAY to write data to a data set. Call READ_ARRAY to read the standardized data back into the array.
proc fcmp outlib = sasuser.ds.functions;
   subroutine standardize(x[*], mean, std);
      outargs x;

      rc = write_array('work._TMP_', x, 'x1');
      dsname = 'work._TMP_';
         colname = 'x1';
         rc = run_macro('standardize', dsname, colname, mean, std);
      array x2[1]_temporary_;
         rc = read_array('work._TMP_', x2);
      if dim(x2) = dim(x) then do;
         do i = 1 to  dim(x);
            x[i] = x2[i];
          end;
         end;
   endsub;
run;
Execute the function for each row in the DATA step.
options cmplib = (sasuser.ds);
data numbers2;
   set numbers;
   array a[5];
   array t[5]_temporary_;
   do i = 1 to 5;
      t[i] = a[i];
   end;
   call standardize(t, 0, 1);
   do i = 1 to 5;
      a[i] = t[i];
   end;
   output;
run;
Write the output.
proc print data=work.numbers2;
run;

Output: HTML

Output from Standardizing Each Row of a Data Set
Output from Standardizing Each Row of a Data Set