I have a PROC EXPORT question that I wonder if you can answer.
I have a SAS dataset with 800+ variables and over 200K observations, and I'm trying to export a subset of variables to a CSV file (i.e. I need all the records, I just don't want all 800 + variables). I can always create a temporary “KEEP” dataset only for the fields I need and run EXPORT on this temporary dataset, but I try to avoid the extra 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 created through PROC EXPORT to contain only x and y. My attempt to solve below does not work.
SAS Code
When I run the following code, I do not 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 each line, and the header still contains all the variables in the data set. In addition, I receive log messages that I should not receive.
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 generated text file ("C: \ test.csv")
x,y,z
1,1,
2,4,
3,9,
4,16,
SAS Magazine
9343 proc export data=ds1(keep=x y)
9344 file='c:\test.csv'
9345 dbms=csv
9346 replace;
9347 quit;
9348
9356 data _null_;
9357 %let _EFIERR_ = 0;
9358 %let _EFIREC_ = 0;
9359 file 'c:\test.csv' delimiter=',' DSD DROPOVER lrecl=32767;
9360 if _n_ = 1 then
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);
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 greatly appreciated. Thank you