Saving the query results to csv

Viewed 363

I'm wondering if it's possible to save the query results to csv? Without creating views.

I have a large table but need only 2 columns from there to process with python then. Maybe someone can help with it?

2 Answers

Here are three ways

ODS

SQL query can be output to an ODS CSV destination. This approach encompasses the widest possibilities of querying.

ods csv file='c:\temp\query-results.csv';

proc sql;
  select name, age 
  from sashelp.class
  where name like 'J%'
  ;
quit;

ods csv close;

EXPORT Procedure

Where clause can be applied using kept columns of 'a large table' (data=)

proc export 
  data = sashelp.class(
    keep=name age
    where = (
      name like 'J%'
    )
  ) 
  replace 
  file = 'c:\temp\class-subset.csv'
  dbms = csv
  ;
run;

DATA _null_

Where statement can be applied using any columns of 'a large table' (SET). The PUT statement manages which columns are output.

data _null_;
  set sashelp.class;
  where name like 'J%';

  file 'c:\temp\subset-per-datastep.csv' dlm=',' dsd;

  if _n_ = 1 then put 'name,age';
  put name age;
run;

I think you can use ods to create file with results, for example:

ods csv file="C:\test.csv" options(delimiter=';');

proc sql;
   select * from sashelp.class;
quit;

ods csv close;
Related