Monday, February 5, 2018

Hack 7.5 Creating SAS Data Sets with Another Operating System’s Data Representation




SAS Programming Professionals,

Did you know that you can create a SAS data set in another operating system's internal data representation format? 

For example, you might be running SAS on Windows and want to create a UNIX SAS data set in a Windows directory for later delivery to a UNIX system.  You can do this using either the OUTREP data set option or the OUTREP LIBNAME option.  Here are examples of both options:

options msglevel=I;

/* Two examples of using the OUTREP LIBNAME option */

libname UNIXDATA "C:\TEMP\UNIXDATA" outrep=hp_ux;

/* 1. Creating a new SAS data set in the "Foreign" data library */

data UNIXDATA.class;
set  sashelp.class;
run;

proc contents data=UNIXDATA.class;
run;

/* 2. Copying a SAS data set to the "Foreign" data library */
proc copy in=sashelp out=UNIXDATA noclone;
      select prdsale;
run;

proc contents data=UNIXDATA.prdsale;
run;

/* 3. Using the OUTREP data set option */

libname UNIXDATA clear;

libname UNIXDATA "C:\TEMP\UNIXDATA";

data UNIXDATA.orsales(outrep=hp_ux);
set  sashelp.orsales;
run;

proc contents data=UNIXDATA.orsales;
run;

In the first LIBNAME OUTREP option example, the newly created CLASS data set will be in HP-UX format, even though it is stored on a Windows directory.  Check out the Data Representation field in the CONTENTS listing.

In the second LIBNAME OUTREP option example, the PRDSALE data set will be in HP-UX format.  But, notice that when the COPY procedure is executed, SAS normally "CLONE"-s the format of the input data set to the output data set.  So, you need to use the NOCLONE option for the new data set (UNIXLIB.PRDSALE) to be in HP-UX format.

The final example illustrates how you can code OUTREP data set option to store UNIXDATA.ORSALES as a UNIX SAS data set, even though it is stored in a Windows directly--as specified in the LIBNAME statement.

Note that with the MSGLEVEL=I option enabled, you will see the following type of message in your SAS log:

INFO: Data file UNIXDATA.CLASS.DATA is in a format that is native to another host, or the file encoding does not match the session encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce performance.


That message lets you know that you have, indeed, been successful in storing a SAS data set in a foreign host’s data representation.  Good job!

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

1 comment:

  1. Started out in 2012, Data Science Central is one of the industry’s leading and fastest growing Internet
    community for data practitioners. Whether it is data science or machine learning or deep learning or
    big data, Data Science Central is a one-stop shop that covers a wide range of data science topics that
    includes technology, tools, data visualisation, code, and job opportunities. Also, many industry experts
    contribute to the community forum for discussion or questions.

    DATA SCIENCE Training in OMR Chennai

    ReplyDelete