SAS Import XLS file without headings - sas

I am importing an XLS file with dataset looking like this:
And my code was as below:
%let dirLSB=/folders/myfolders/sasuser.v94/;
proc import datafile="&dirLSB.OnionRing.xls" out=sales replace;
run;
proc print data=sales label;
run;
But the result showed the first row had been treated as headings and the
row data for the first row "Columbia Peaches" was missing.
It should have been four rows but in the end, only three rows were present.
Are there any suggestions?
Thanks a lot!!!

Just add the getnames=no; statement to your proc import step.
proc import datafile="&dirLSB.OnionRing.xls" out=sales replace;
getnames=no;
run;

Related

Effectively read multiple excel sheets from an xlsx excel workbook in SAS 9.4M7

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;

SAS import single cell value from Excel spreadsheet

I am using sas 9.2 and i want top import a cell value from a spreadsheet
the value i want are F4 located in the sheet "data beskrivelse"
This i what i do:
proc import out=TESTER
datafile="&request_in"
dbms=XLS replace;
RANGE="data beskrivelse$F4:F5";
run;
And it works ok, but the result is that the column name gets the value in F4 and
the data from F5 is imported.
So my sas dataset has ha column where the header name are the value of F4.
if i change the range in proc import to
proc import out=TESTER
datafile="&request_in"
dbms=XLS replace;
RANGE="data beskrivelse$F4:F4";
run;
I'll get the whole column imported from the spreadsheet.
So basically i want the value from F4 to be imported into my sas dataset in variable A
Oh i managed to figure it out,
the solution is to use the getnames option in proc imort
it says wether to import as names or not.
so i changed to this
proc import out=TESTER
datafile="&request_in"
dbms=XLS replace;
getnames=NO;
RANGE="data beskrivelse$F4:F4";
run;
and i get the value in as data in my 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 To select All Rows from a SAS data set that Match at least one value in another SAS Data set

I am trying to read the ticker names of 100 stocks / ETFs from one CSV file. I have two CSV files, one contains the data for all stocks / etfs over a 90 day period. The second contains the name of the 100 stock/etf tickers I am interested in selecting. Below is my code, WORK.ETFnames is the one column data set that contains the 100 ETF names I want to select from the fulldata. How can I use this list of names to correctly select the desired data. In WORK.FULLdata the names are stored in a column called "Ticker". I have already sorted the data by type (either ETF or Stock) but cant figure out how to select the rows I am actually interested in from these tables. Thank you!
PROC IMPORT OUT=WORK.Fulldata
DATAFILE="/folders/myshortcuts/myfolder/q2_2012_all.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;
PROC IMPORT OUT = WORK.ETFnames
DATAFILE = "/folders/myshortcuts/myfolder/ETFs.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;
PROC SQL;
CREATE TABLE stocks AS
SELECT *
from Fulldata
where Security EQ "Stock";
QUIT;
PROC SQL;
CREATE TABLE ETF AS
SELECT *
from Fulldata
where Security EQ "ETF"
QUIT;
You might want to try merging the two datasets, and only accepting those that have matching "Ticker" values. I'm going to assume that the dataset ETFnames has the names stored under variable "Ticker" too.
PROC IMPORT OUT= WORK.Fulldata
DATAFILE= "/folders/myshortcuts/myfolder/q2_2012_all.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;
PROC IMPORT OUT= WORK.ETFnames
DATAFILE= "/folders/myshortcuts/myfolder/ETFs.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;
PROC SORT DATA=WORK.Fulldata OUT=WORK.Fulldatasort;
BY Ticker;
RUN;
PROC SORT DATA=WORK.EFTnames OUT=WORK.EFTnamessort;
BY Ticker;
RUN;
DATA WORK.Partdata;
MERGE WORK.Fulldatasort WORK.EFTnamessort(in=A);
BY Ticker;
IF A;
RUN;
PROC SQL;
CREATE TABLE stocks AS
SELECT *
from Partdata
Where Security EQ "Stock";
QUIT;
PROC SQL;
CREATE TABLE ETF AS
SELECT *
from Partdata
Where Security EQ "ETF"
QUIT;
As far as I can tell this will give you the desired result. You could also do a join in your PROC SQL statements instead of a MERGE, but the MERGE is easier to write IMO.

How to import an excel to sas with getnames = no?

I want to specify new names so I use getnames=no property:
data mylib.test;
infile "C:\Users\test.xlsx" ;
input var1 $ Opened_Date mmddyy8. salary dollar9.2;
DBMS=EXCEL ;
range="Sheet5$";
getnames=no;
mixed=no;
scantext=yes;
usedate=yes;
scantime=yes;
datarow=3;
run;
But this does not import anything
PS the following code with getnames=yes works fine . This means that there is no problems with excel file . But i don't want to use yes I need getnames=no
PROC IMPORT OUT= WORK.TEST
DATAFILE= "C:\Userstest.xlsx"
DBMS=EXCEL REPLACE;
RANGE="Sheet5$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
PROC IMPORT OUT= WORK.TEST
DATAFILE= "C:\Userstest.xlsx"
DBMS=EXCEL REPLACE;
RANGE="Sheet5$";
GETNAMES=NO;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
The data step is not helpful in this case. You can't import an excel file that way (practically speaking).
After this, you then create a data step and can rename things however you want from the generic names initially assigned by the PROC IMPORT.
An alternative that looks like the data step method is libname access.
libname myexcel excel "c:\Usertest.xlsx" getnames=no scantext=yes mixed=no usedate=yes scantime=yes;
Then you can access the file like
data test;
set myexcel,'Sheet5$'n;
rename f1=var1 f2=opened_date (...more...);
run;
I tend to use PROC IMPORT as it's a bit easier to understand for others, but both are equivalent in how they work (PROC IMPORT creates this libname for you, basically).