I want to import a file in CSV format, stored on a local network drive to the SAS Enterprise Guide via code. How to make ?
proc import
dbms=csv /* specify you load comma separated data */
datafile="C:\temp\test.csv" /* fill in your data file */
out=TEST /* specify de dataset to load it to */
replace; /* so TEST will be replaced if it exists */
/* getnames=no; /* only if your file does not have headers */
run;
Related
I am trying to read multiple sheets from an excel workbook (xlsx format) in SAS. Instead of using two separate proc imports, is there a way to simultaneously read multiple excel sheets from an excel workbook? My code thus far is as follows:
proc import datafile= "&loc.\&exid..xlsx"
out=exp
dbms=xlsx replace;
sheet="Sheet1";
run;
proc import datafile= "&loc.\&exid..xlsx"
out=dt
dbms=xlsx replace;
range="'Sheet5'$A2:AB10000";
getnames=yes;
run;
It is taking ~1.40 secs to read both of these excel sheets from one excel workbook, how do I reduce the time it takes to read xlsx workbook in SAS.
If you have SAS/CONNECT, you can run the two imports in parallel with rsubmit. I tested this out on an Excel file and it did not give any simultaneous access errors. Since each import is only about 1.4 seconds, this might take longer in aggregate since it needs to spin up two new SAS sessions to run each import.
options autosignon = yes
connectwait = no
sascmd = '!sascmd'
;
libname worklib "%sysfunc(getoption(work))";
/* Send over macro variables to rsubmit sessions */
%syslput _USER_ / remote=session1;
%syslput _USER_ / remote=session2;
rsubmit remote=session1 inheritlib=(worklib);
proc import datafile= "&loc.\&exid..xlsx"
out=worklib.exp
dbms=xlsx replace;
sheet="Sheet1";
run;
endrsubmit;
rsubmit remote=session2 inheritlib=(worklib);
proc import datafile= "&loc.\&exid..xlsx"
out=worklib.dt
dbms=xlsx replace;
range="'Sheet5'$A2:AB10000";
getnames=yes;
run;
endrsubmit;
I want to use sas enterprise guide 8.3 import csv file into a relational database.
I launch the import data wizard, locate the csv file in local folder(Windows OS), then find the library of the relational database. From the GUI, I cannot setup the mapping between the source and the target. the target is a table in relational database which has more fields than the csv file.
The import data wizard failed because the wizard tried to replace the table which already exists.
so I cannot use import data wizard to achieve my goal? only with following code ?
Libname TD teradata userid=xxxx password=xxxx fastload=Yes
proc import
dbms=csv /* specify you load comma separated data */
datafile="C:\temp\test.csv" /* fill in your data file */
out=TD; /* specify de dataset to load it to */
/* replace; so TEST will not replaced if it exists */
run;
I am not sure the code above can solve my problem: out =TD does not specify the table name, just specify the target database. Thanks you very much!
It sounds like you are trying to append this .csv file to an existing Teradata table using SAS. You can do this in two steps:
Import the .csv file into SAS as a temporary SAS table (alternatively, you can import this as a temporary table in Teradata if you'd like)
Append the temporary SAS table to the master Teradata table
The SAS Teradata Access Engine will handle all the translation of code from one database to another:
Libname TD teradata userid=xxxx password=xxxx fastload=Yes;
proc import
dbms = csv
file = "C:\temp\test.csv"
out = tempTable;
replace;
run;
proc append base = td.have
data = tempTable
force;
run;
Or you could use SQL to append:
proc sql;
insert into td.have
select * from tempTable
;
quit;
How can I protect my excel file with a password, because in sas EG not supported DDE.
I'm using this, but I dont have the convert.vbs, so where can i get it?
/***********************************************/
/* Create a test worksheet to password protect */
/***********************************************/
ods tagsets.excelxp file="c:\temp.xml";
proc print data=sashelp.class;
run;
ods tagsets.excelxp close;
/*****************************************************************/
/* Create and excecute a script file using the input XML file */
/* and the converted XLSX file. The value 51 is needed for XLSX. */
/* Use the value of 1 for XLS files in the SaveAs command. */
/*****************************************************************/
%let XMLfile = c:\temp.xml;
%let ExcelFile = c:\temp.xlsx;
%let VBscript = ***c:\convert.vbs***;
%let password=test;
data _null_;
file "&vbscript" lrecl=200;
put 'Dim xlApp, xlWkb, SourceFile, TargetFile';
put 'Set xlApp = CreateObject("excel.application")';
put 'SourceFile="' "&XMLfile" '"';
put 'Set xlWkb = xlApp.Workbooks.Open(SourceFile)';
put 'TargetFile="' "&ExcelFile" '"';
put 'xlApp.DisplayAlerts=false';
put "xlWkb.SaveAs TargetFile, 51,""&password""";
put 'xlApp.DisplayAlerts=true';
put 'xlWkb.close';
run;
options noxwait noxsync;
x "cscript ""&vbscript""";
It appears that your code creates the file c:\convert.vbs for you and then runs it. You just need to remove the asterisks so that it's a valid file path.
If you don't have DDE it's unlikely that code will work for you either.
The last line,
x "cscript ""&vbscript""";
Is a command to your operating system. Often if DDE is disabled, this type of functionality is also disabled. You can check this by examining the XCMD option.
proc options option=xcmd;
run;
If XCMD is enabled you'll see:
XCMD Enables the X command in SAS.
You can only change that setting at start up and sometimes need to be an administrator as well.
We can import an XLS file using namerow and startrow, like in this example :
%let dir_n=TheDir_name;
%let fichimp=file_name.xls;
PROC IMPORT DATAFILE= "&dir_n.\&file_name."
out=want
dbms=xls replace;
sheet=theSheet_name;
getnames=no;
namerow=2;
startrow=3;
run;
I have read : To import XLSX file, use RANGE if the data is not starting on the first line.
Is there similar option to STARTROW to import XLSX file starting from a specific row?
No, there is not. dbms=XLSX only has a limited set of options, listed in the documentation: GETNAMES, SHEET, and RANGE.
EXCEL has a few more options (including DBDSOPTS which opens up several database-type options), but still uses range to control what is read in.
I could navigate to the library path in the file explorer and search for my dataset.sas7bdat file and look at it's size.
But that's not practical. I'm not even sure where the WORK library is located and even if I did it's on a remote server making it complicated to access.
Is it possible to print the size/weight of a dataset in the log or a report?
using SAS EG 7.1 and SAS 9.3
Motive : I want to do that because I will try to reduce a dataset size and I would like to know how much I gained.
You can use the SQL dictionary view dictionary.tables :
/* Return libref, dataset name, # records, filesize & % compression */
proc sql ;
create table size1 as
select libname, memname, nlobs, filesize, pcompress
from dictionary.tables
where libname = 'WORK'
and memname = 'MYDATA'
;
quit ;
As long as your library uses the BASE engine, you can use the pathname() function to find it. After that you can use the sashelp views to get the filesize. You could also use os commands, but for that you need x command enabled.
The following demonstrates:
%let libds=WORK.SAMPLE;
/* create demo data */
data &libds;
retain string 'blaaaaaaaah';
do x=1 to 10000;
output;
end;
run;
/* extract library and datasaet from libds variable */
%let lib=%scan(&libds,1,.);
%let ds =%scan(&libds,2,.);
/* set up filename to point directly at the dataset */
/* note - if you have indexes you also need to do */
/* this for the .sas7bndx extension */
filename fref "%sysfunc(pathname(&lib))/&ds..sas7bdat";
/* query dictionary view for file attributes */
data _null_;
set sashelp.vextfl(where=(fileref='FREF'));
filesize=filesize/(1024**2); /* size in MB */
putlog filesize= 'MB';
run;
/* clear libref */
filename fref clear;