Saving the query results to csv - sas

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?

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

Missing the last few column names when exporting to CSV file

I am trying to export a dataset in my Library/Work. It shows normal in SAS. However when I export the data as CSV or txt file (either from right click -> export, or use SAS code), the last few column names were missing (showing empty in CSV), while the values were kept. The column names missing are all in the format of "Log_xxx" but some the same-format columns were exported correctly. There're around 4000+ columns in my dataset.
The code I've tried is like:
proc export data=logdata
outfile="path.csv"
dbms=csv
replace;
run;
I've exported many datasets before, but it's the first time I have this kind of problem. I've tried to restart SAS and it's still not working.
I simply wanted to export the whole dataset completely with all column names and values.
Do you have any ideas?
I don't think it is PROC EXPORT that is the issue. You have to tell SAS that you want to write lines that are longer then 32,767 bytes (the default setting for the LRECL option).
This code works:
data test;
array longname [3500] ;
run;
filename csv temp lrecl=1000000 ;
proc export data=test dbms=csv file=csv ;
run;
So change your code to set the LRECL long enough for all of the variable names.
filename csv "path.csv" lrecl=1000000 ;
proc export data=logdata
outfile=csv
dbms=csv
replace
;
run;
Based on this post, your header is likely exceeding 32k characters, which causes the issues.
Solution is to manually create the file without proc export, or proc export to XLSX doesn't appear to have the issue.
*Create demo data;
data class;
set sashelp.class;
label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;
proc sql noprint;
create table temp as
select name as _name_, label as _label_
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
select nliteral(name) into :varList separated by ' '
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
quit;
data _null_;
file "&sasforum.\datasets\TwoLinesHeader.csv" dsd lrecl = 40000;
set class;
if _n_ = 1 then do;
do until(eof);
set temp end=eof;
put _name_ #;
end;
put;
end;
put (&varList) (:);
run;

Exporting data from SAS to Excel with a custom file name

I need to export a data set from SAS to Excel 2013 as a .csv file. However, I need the file name to be dynamic. In this instance, I need it to appear as:
in_C000000_013117_65201.csv
where the string, "in_C000000_" will remain constant, the string "013117_" will be the current day's date, and the string "65201" will be the row count of the data set itself.
Any help that you can provide would be much appreciated!
Thanks!
Here's a modified macro I wrote in the past that does almost exactly what you're asking for. If you want to replace sysdate with a date in your desired format, that's easy to do as well:
%let path = [[desired destination]];
%macro exporter(dataset);
proc sql noprint;
select count(*) into: obs
from &dataset.;
quit;
data temp;
format date mmddyy6.;
date = today();
run;
proc sql noprint;
select date format mmddyy6. into: date_formatted
from temp;
quit;
proc export data = &dataset.
file = "&path.in_C000000_&date_formatted._%sysfunc(compress(&obs.)).csv"
dbms = csv replace;
run;
%mend exporter;
%exporter(your_dataset_here);
Produces datasets in the format: in_C000000_020117_50000.csv

how to print dataset without formats in SAS

I ´have a dataset with formats attached to it and I dont want to remove the formats from the dataset and when I use proc freq or proc print, I want the original values and not the formats attached.
Proc print data=mylib.data;
run;
is there any format=no option?
proc freq data=mylib.data;
tables gender;
format?????
run;
You can remove a format by specifying a null format on the PROC strep:
proc freq data=mylib.data ;
tables gender ;
format _ALL_ ;
run ;
_ALL_ is a list of all variables in the dataset.

PROC EXPORT outfile row 2

I'm trying to export the column names of a sas data to a xlsx file but need the data to be copied starting in the 2nd row of the excel file. What I have right now:
PROC EXPORT DATA= mylib.test
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
sheet = "test1";
range = "test1$A2:BE2000";
run;
However, I get an error indicating that the RANGE statement is not supported and is ignored in Export procedure
Any suggestions?
Try the data set option FIRSTOBS.
PROC EXPORT DATA= mylib.test (firstobs=2)
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
run;
Edit: If by"starting in the 2nd row" you mean to output the data without the variable names, then you have to use PUTNAMES=NO;
PROC EXPORT DATA= mylib.test
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
PUTNAMES=NO;
run;
Load your table with a blank row as first row. Try writing the table to excel file then. It should work.
Proc sql
insert into test
values('',.,'')
quit;
Proc sort data=test;
by _all_;
run;
Options missing='';
proc export data=test outfile='/home/libname/new.xlsx'
dbms=excel replace;
putnames=no;
run;

How can I create SAS html files with names corresponding to a dataset field

I'm trying to create a file for each record in my SAS dataset. I have been able to succesfully do this using SAS ODS output by using the "newfile=page" option and then simply running a proc report on the dataset. The problem I'm having is that this results in gerneric, sequentially numbered file names (test, test1, test2, etc...). I'd like to name each file based on a variable in the dataset. In the example below, I'd like the file names to be titled based on the "seq_nbr" in the dataset.
ods html path = "C:\test\"
file = 'test.html'
contents = 'contents.html
frame = 'frame.html'
code = (url="C:\test\javascript.js")
newfile=page;
proc report data = test2 nowindows headline headskip;
column tDate tTime created_by cmtText;
by seq_nbr;
run;
You need a macro for this
%macro doit;
proc sql;
select count(*) into :n from test2 ;
quit;
%do i=1 %to &n;
data _null_;
if _n_=&i call symput('name',seq_nbr);
run;
proc export data=something outfile="&name";
run;
%end;
%mend;
%doit;