Testing Your Programs

Limiting Observations

Remember that you can use the OBS= option in the INFILE statement to limit the number of observations that are read or created during the execution of the DATA step.
data work.update;
   infile invent obs=10;
   input Item $ 1-13 IDnum $ 15-19 
         InStock 21-22 BackOrd 24-25; 
   Total=instock+backord;  
run;
When processed, this DATA step creates the work.update data set with variables but with only 10 observations.

PUT Statement

When the source of program errors is not apparent, you can use the PUT statement to examine variable values and to print your own message in the log. For diagnostic purposes, you can use IF-THEN/ELSE statements to conditionally check for values. For more information about IF-THEN/ELSE statements, see Creating and Managing Variables.
data work.test;
   infile loan; 
   input Code $ 1 Amount 3-10 Rate 12-16 
         Account $ 18-25 Months 27-28; 
   if code='1' then type='variable'; 
   else if code='2' then type='fixed'; 
   else type='unknown';
   if type='unknown' then put 'MY NOTE: invalid value:' code=;
run;
In this example, if Code does not have the expected values of 1 or 2, the PUT statement writes a message to the log:
Log 7.4 SAS Log
MY NOTE: invalid value: Code=V
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28.
      The maximum record length was 28. 
NOTE: The data set WORK.TEST has 9 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds
Syntax, PUT statement:
PUT specification(s);
specification specifies what is written, how it is written, and where it is written. Here are examples:
  • a character string
  • one or more data set variables
  • the automatic variables _N_ and _ERROR_
  • the automatic variable _ALL_

Example: Character Strings

You can use a PUT statement to specify a character string to identify your message in the log. The character string must be enclosed in quotation marks.
put 'MY NOTE: The condition was met.';
Log 7.5 SAS Log
MY NOTE: The condition was met.
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28. 
      The maximum record length was 28.
NOTE: The data set WORK.TEST has 9 observations and 6 variables. 
NOTE: DATA statement used (Total process time):
      real time            0.01 seconds
      cpu time             0.01 seconds

Example: Data Set Variables

You can use a PUT statement to specify one or more data set variables to be examined for that iteration of the DATA step:
put 'MY NOTE: invalid value: 'code type;
Log 7.6 SAS Log
MY NOTE: invalid value: V unknown
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28.
      The maximum record length was 28.
NOTE: The data set WORK.TEST has 9 observations and 6 variables. 
NOTE: DATA statement used (Total process time):
      real time            0.00 seconds
      cpu time             0.00 seconds
Note that when you specify a variable in the PUT statement, only its value is written to the log. To write both the variable name and its value in the log, add an equal sign (=) to the variable name.
put 'MY NOTE: invalid value: 'code= type=;
Log 7.7 SAS Log
MY NOTE: invalid value: Code=V type=unknown
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28.
      The maximum record length was 28.
NOTE: The data set WORK.TEST has 9 observations and 6 variables. 
NOTE: DATA statement used (Total process time):
      real time            0.01 seconds
      cpu time             0.00 seconds

Example: Automatic Variables

You can use a PUT statement to display the values of the automatic variables _N_ and _ERROR_. In some cases, knowing the value of _N_ can help you locate an observation in the data set:
put 'MY NOTE: invalid value: ' 
     code= _n_= _error_=;
Log 7.8 SAS Log
MY NOTE: invalid value: Code=V _N_=3 _ERROR_=0
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28.
      The maximum record length was 28.
NOTE: The data set WORK.TEST has 9 observations and 6 variables. 
NOTE: DATA statement used (Total process time):
      real time            0.01 seconds
      cpu time             0.01 seconds
You can also use a PUT statement to write all variable names and variable values, including automatic variables, to the log. Use the _ALL_ specification:
put 'MY NOTE: invalid value: ' _all_ ;
Log 7.9 SAS Log
MY NOTE: invalid value: Code=V Amount=10000 Rate=10.5 Account=101-1289 Months=8
type=unknown _ERROR_=0 _N_=3
NOTE: 9 records were read from the infile LOAN.
      The minimum record length was 28.
      The maximum record length was 28.
NOTE: The data set WORK.TEST has 9 observations and 6 variables. 
NOTE: DATA statement used (Total process time):
      real time            0.01 seconds
      cpu time             0.01 seconds

Example: Conditional Processing

You can use a PUT statement with conditional processing (that is, with IF-THEN/ELSE statements) to flag program errors or data that is out of range. In the example below, the PUT statement is used to flag any missing or zero values for the variable Rate.
data finance.newcalc; 
   infile newloans; 
   input LoanID $ 1-4 Rate 5-8 Amount 9-19; 
   if rate>0 then 
      Interest=amount*(rate/12);
   else put 'DATA ERROR ' rate= _n_=; 
run;
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.178