OUTPUT Method

Creates one or more data sets each of which contain the data in the hash object.
Applies to: Hash object

Syntax

Arguments

rc
specifies whether the method succeeded or failed.
A return code of zero indicates success; a nonzero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.
object
specifies the name of the hash object.
DATASET: 'dataset'
specifies the name of the output data set.
The name of the SAS data set can be a character literal or character variable. The data set name can also be enclosed in double quotation marks. When specifying the name of the output data set, you can use SAS data set options in the DATASET argument tag. Macro variables must be enclosed in double quotation marks.
datasetoption
specifies a data set option.
For complete information about how to specify data set options, see Syntax in SAS Data Set Options: Reference.

Details

Hash object keys are not automatically stored as part of the output data set. The keys must be defined as data items by using the DEFINEDATA method to be included in the output data set.
If you use the ordered: 'yes' or ordered: 'ascending' argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then the data items are written to the data set in ascending key-value order. If you use the ordered: 'descending' argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then the data items are written to the data set in descending key-value order. If you do not use the ordered argument tag, the order is undefined.
When specifying the name of the output data set, you can use SAS data set options in the DATASET argument tag. Data set options specify actions that apply only to the SAS data set with which they appear. They let you perform the following operations:
  • renaming variables
  • selecting a subset of observations based on the observation number for processing
  • selecting observations using the WHERE option
  • dropping or keeping variables from a data set loaded into a hash object, or for an output data set that is specified in an OUTPUT method call
  • specifying a password for a data set.
The following example uses the WHERE data set option to select specific data for the output data set named OUT:
data x;
 do i = 1 to 20;
   output;
 end;
run;
 
/* Using the WHERE option. */
data _null_;
  length i 8;
  dcl hash h();
  h.definekey(all: 'y');
  h.definedone();
  h.output(dataset: 'out (where =( i < 8))');
run;
The following example uses the RENAME data set option to rename the variable J to K for the output data set named OUT:
data x;
 do i = 1 to 20;
   output;
 end;
run;
/* Using the RENAME option. */
data _null_;
  length i j 8;
  dcl hash h();
  h.definekey(all: 'y');
  h.definedone();
  h.output(dataset: 'out (rename =(j=k))');
run; 
For a list of data set options, see SAS Data Set Options: Reference.
Note: When you use the OUTPUT method to create a data set, the hash object is not part of the output data set. In the following example, the H2 hash object will be omitted from the output data set.
data _null_;
   length k 8;
   length d $10;
   declare hash h2();
   declare hash h(ordered: 'y');
   h.defineKey('k');
   h.defineData('k', 'd', 'h2');
   h.defineDone();
   k = 99;
   d = 'abc';
   h.add();
   k = 199;
   d = 'def';
   h.add();
   h.output(dataset:'work.x');
run;

Example

Using the data set ASTRO that contains astronomical data, the following code creates a hash object with the Messier (OBJ) objects sorted in ascending order by their right-ascension (RA) values and uses the OUTPUT method to save the data to a data set.
data astro;
input obj $1-4 ra $6-12 dec $14-19;
datalines;
 M31 00 42.7 +41 16
 M71 19 53.8 +18 47
 M51 13 29.9 +47 12
 M98 12 13.8 +14 54
 M13 16 41.7 +36 28
 M39 21 32.2 +48 26
 M81 09 55.6 +69 04
M100 12 22.9 +15 49
 M41 06 46.0 -20 44
 M44 08 40.1 +19 59
 M10 16 57.1 -04 06
 M57 18 53.6 +33 02
  M3 13 42.2 +28 23
 M22 18 36.4 -23 54
 M23 17 56.8 -19 01
 M49 12 29.8 +08 00
 M68 12 39.5 -26 45
 M17 18 20.8 -16 11
 M14 17 37.6 -03 15
 M29 20 23.9 +38 32
 M34 02 42.0 +42 47
 M82 09 55.8 +69 41
 M59 12 42.0 +11 39
 M74 01 36.7 +15 47
 M25 18 31.6 -19 15
;
run;
data _null_;
   if _N_ = 1 then do;
      length obj $10;
      length ra $10;
      length dec $10;
      /* Read ASTRO data set as ordered */
   declare hash h(hashexp: 4, dataset:"work.astro", ordered: 'yes');
      /* Define variables RA and OBJ as key and data for hash object */
      h.defineKey('ra');
      h.defineData('ra', 'obj');
      h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(ra, obj);
end;
/* Create output data set from hash object */
rc = h.output(dataset: 'work.out');
run;

proc print data=work.out;
   var ra obj;
   title 'Messier Objects Sorted by Right-Ascension Values';
run;
Messier Objects Sorted by Right-Ascension Values
Messier Objects Sorted by Right-Ascension Values