Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS: Limiting variables in PROC EXPORT

Tags:

sas

I have a PROC EXPORT question that I am wondering if you can answer.

I have a SAS dataset with 800+ variables and over 200K observations and I am trying to export a subset of the variables to a CSV file (i.e. I need all records; I just don’t want all 800+ variables). I can always create a temporary dataset “KEEP”ing just the fields I need and run the EXPORT on that temp dataset, but I am trying to avoid the additional step because I have a large number of records.

To demonstrate this, consider a dataset that has three variables named x, y and z. But, I want the text file generated through PROC EXPORT to only contain x and y. My attempt at a solution below does not quite work.

The SAS Code

When I run the following code, I don’t get exactly what I need. If you run this code and look at the text file that was generated, it has a comma at the end of every line and the header includes all variables in the dataset anyway. Also, I get some messages in the log that I shouldnt be getting.

data ds1;
      do x = 1 to 100;
            y = x * x;
            z = x * x * x;
            output;
      end;
run;

proc export data=ds1(keep=x y)
      file='c:\test.csv'
      dbms=csv
      replace;
quit;

Here are the first few lines of the text file that was generated ("C:\test.csv")

x,y,z
1,1,
2,4,
3,9,
4,16,

The SAS Log

9343  proc export data=ds1(keep=x y)
9344      file='c:\test.csv'
9345      dbms=csv
9346      replace;
9347  quit;

9348   /**********************************************************************
9349   *   PRODUCT:   SAS
9350   *   VERSION:   9.2
9351   *   CREATOR:   External File Interface
9352   *   DATE:      30JUL12
9353   *   DESC:      Generated SAS Datastep Code
9354   *   TEMPLATE SOURCE:  (None Specified.)
9355   ***********************************************************************/
9356      data _null_;
9357      %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
9358      %let _EFIREC_ = 0;     /* clear export record count macro variable */
9359      file 'c:\test.csv' delimiter=',' DSD DROPOVER lrecl=32767;
9360      if _n_ = 1 then        /* write column names or labels */
9361       do;
9362         put
9363            "x"
9364         ','
9365            "y"
9366         ','
9367            "z"
9368         ;
9369       end;
9370     set  DS1(keep=x y)   end=EFIEOD;
9371         format x best12. ;
9372         format y best12. ;
9373         format z best12. ;
9374       do;
9375         EFIOUT + 1;
9376         put x @;
9377         put y @;
9378         put z ;
9379         ;
9380       end;
9381      if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
9382      if EFIEOD then call symputx('_EFIREC_',EFIOUT);
9383      run;

NOTE: Variable z is uninitialized.
NOTE: The file 'c:\test.csv' is:
      Filename=c:\test.csv,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=30Jul2012:12:05:02,
      Create Time=30Jul2012:12:05:02

NOTE: 101 records were written to the file 'c:\test.csv'.
      The minimum record length was 4.
      The maximum record length was 10.
NOTE: There were 100 observations read from the data set WORK.DS1.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds


100 records created in c:\test.csv from DS1.


NOTE: "c:\test.csv" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
      real time           0.12 seconds
      cpu time            0.06 seconds

Any ideas how I can solve this problem? I am running SAS 9.2 on windows 7.

Any help would be appreciated. Thanks.

  • Karthik
like image 998
Karthik Avatar asked Jul 30 '12 18:07

Karthik


1 Answers

Based in Itzy's comment to my question, here is the answer and this does exactly what I need.

proc sql;
    create view vw_ds1 as 
        select x, y from ds1;
quit;

proc export data=vw_ds1
    file='c:\test.csv'
    dbms=csv
    replace;
quit;

Thanks for the help!

like image 112
Karthik Avatar answered Sep 23 '22 14:09

Karthik