I'm trying to access an xlsx file stored on SAS Drive from a SAS Studio session, all on Viya 4.
This piece of SAS documentation suggests that it works on Viya just like in SAS 9.4: https://go.documentation.sas.com/doc/en/pgmsascdc/v_031/acpcref/titlepage.htm
However it doesn't go into any details or examples and when I try this code it runs successfully but the resulting library is empty (and in fact changing the path to a non-existing file won't change anything, so I doubt that the file access was working in the first place):
libname myxls xlsx "/folders/myfolder/file.xlsx";
proc contents data=myxls._all_;
run;
You need to use the filesrvc access method to access files on SAS Drive.
filename myfile filesrvc
folderpath = '/folders/myfolder/'
filename = 'file.xlsx'
;
You cannot have direct libname access to files stored on SAS Drive, but you can import them into SAS using proc import:
proc import
file = myfile
dbms = 'xlsx'
out = myxls
replace;
run;
If you have persistent storage then you certainly can use standard libname access so long as that file is in that storage location.
If you don't and you'd like to still have libname access, one workaround is to physically copy the xlsx file to your WORK directory, then assign a new libname statement:
filename source filesrvc
folderpath = '/folders/myfolder/'
filename = 'file.xlsx'
;
filename dest "%sysfunc(pathname(work))/file.xlsx";
/* Use fcopy to copy from SAS Drive to WORK */
%let rc = %sysfunc(fcopy(source, dest));
libname myxls xlsx "%sysfunc(pathname(work))/file.xlsx";
More information on filesrvc:
https://go.documentation.sas.com/doc/en/pgmsascdc/v_031/lestmtsglobal/p0qapul7pyz9hmn0zfoefj0c278a.htm
You get empty library if the path isn't correct. Or if your system doesn't understand your path. Remember, you have to have your folders on a server and not on your personal disc.
libname myxls "path\myfolder\file.xlsx";
proc contents data=myxls._all_;
run;
This works for me just fine. Maybe your system had issues with using / instead of \ (my setup certainly doesn't work with / ). Or path wasn't correct. Can't be sure unless we get more details about your setup.
Related
I am combining two XPT files with following program code:
LIBNAME DM XPORT '/home/u62208181\DEMO.XPT';
LIBNAME QX XPORT '/home/u62208181\CDQ.XPT';
LIBNAME OUT '/home/u62208181';
DATA OUT.CDQ_DEMO;
MERGE DM.DEMO (KEEP=SEQN RIDAGEYR RIAGENDR)
QX.CDQ (IN=A);
BY SEQN;
IF A;
RUN;
Even though files are in folder- SAS show this error
Try converting the XPT to SAS data sets first.
Note that Unix is case sensitive, if you still get an error right click on the XPT file in the folder and copy the path from properties and paste that into your path.
LIBNAME DM XPORT '/home/u62208181/DEMO.XPT';
LIBNAME QX XPORT '/home/u62208181/CDQ.XPT';
LIBNAME OUT '/home/u62208181';
PROC COPY IN=DM OUT= OUT;
SELECT DEMO;
RUN;
PROC COPY IN=QX OUT=OUT;
SELECT CDQ;
RUN;
DATA OUT.CDQ_DEMO;
MERGE OUT.DEMO (KEEP=SEQN RIDAGEYR RIAGENDR)
OUT.CDQ (IN=A);
BY SEQN;
IF A;
RUN;
The \ character in Unix is used to "escape" the following character. So this path
/home/u62208181\DEMO.XPT
Is the same as
/home/u62208181DEMO.XPT
Which should not exist since only user directories should be in the /home folder and if it did exist you probably would not have access to it since it is not in your home directory.
Try using / instead.
/home/u62208181/DEMO.XPT
Note that the LIBNAME statements work because SAS does not know whether you are expecting to read from an existing file or create a new file. It is only when the code actually tries to read from the library that SAS warns you that the file does not yet exist.
I am new to SAS but used to working in python.
In python, I can read an xlsx file from a static Box.com link by calling pandas read excel function on the Box link.
pd.read_excel("https://box.com/shared/static/<url>.xlsx")
I'm hoping to do something similar in SAS. When I try
PROC IMPORT DATAFILE="https://box.com/shared/static/<url>.xlsx"
OUT=WORK.MYEXCEL
DBMS=XLSX
REPLACE;
RUN;
SAS tries to look for a document with the name "https://box.com/shared/static/.xlsx".
When I try
filename xlsxFile http "https://box.com/shared/static/<url>.xlsx";
PROC IMPORT DATAFILE=xlsxFile
OUT=WORK.MYEXCEL
DBMS=XLSX
REPLACE;
RUN;
I get
ERROR: This "filename URL" access method is not supported by "proc import". Please copy the file to local disk before running the
procedure.
Is there an easy way to have SAS access files from this type of URL? I've checked these few threads:
https://communities.sas.com/t5/General-SAS-Programming/import-excel-file-from-the-web/td-p/134158
https://communities.sas.com/t5/SAS-Programming/Importing-XLSX-from-URL-Issue/td-p/446758
https://communities.sas.com/t5/SAS-Programming/proc-import-xlsx-from-url/td-p/635834
And it seems like I may need to do some work to get SAS to create a file object from the URL but I don't quite understand how the code is working and when I naively try something similar:
filename xlsxFile http "https://box.com/shared/static/<url>.xlsx";
data file;
n=-1;
infile xlsxFile recfm=s nbyte=n length=len;
input;
file "file_name.xlsx" recfm=n;
put _infile_ $varying32767. len;
run;
PROC IMPORT OUT= input DATAFILE= "file_name.xlsx"
DBMS=xls REPLACE;
SHEET="sheet_name";
GETNAMES=YES;
RUN;
I get the following error:
Spreadsheet isn't from Excel V5 or later. Please open it in Excel and Save as V5 or later
Requested Input File Is Invalid
ERROR: Import unsuccessful. See SAS Log for details.
Any help would be appreciated. The reason I'd like to do it this way is because the files at the static links are updated daily and I want to avoid having to copy files to the SAS server every day. So if this won't work I am also interested in other work arounds that would accomplish the same thing.
I can obviously write a script that will fetch the updated files and write them to the server where SAS can access them as needed, but wanted to see if I could get this to work first.
Using the Box API from within SAS would also be another option if anyone has successfully gotten that to work. As I mentioned I am new to SAS so trying to access the API seemed like it would be too difficult at the moment.
Thank you!
You'll definitely need to download the file. As Tom notes in comments, make sure to check this is really an xlsx file; but assuming it is, some suggestions for dealing with it.
Copying it over by hand like you're doing is doable, and I'll put some code that works at the bottom of the answer, but it's way overkill nowadays - you're probably reading decade-old papers from before the modern day of SAS. In particular, you should use PROC HTTP to GET the file, rather than using the URL directly - it's just much easier that way.
Another possibility is you could use python for this! Regardless of your SAS setup, you can use python to script SAS, or use SAS to run Python, very easily nowadays. SASPy project (on github) for SAS 9, or the SAS SWAT package (also on github I believe) for connecting to SAS Viya, lets you very easily talk back and forth with Python and SAS, even in production - I do it all the time. And on top of that, you can even write SAS user-written functions in python now - so there're a bunch of ways to get your Python into SAS, if you want. I am a SAS (primary) developer, but I use Python for web-related stuff, since it's just better at it. See my paper for more details, or lots of other resources on the subject.
Assuming you just want to import the file and don't want to keep track of the excel file ultimately, you can just read it from a temp filename, which will clean itself up afterwards:
filename _httpin temp;
proc http method="get"
url="https://github.com/snoopy369/SASL/raw/master/Excel%20Precision.xlsx"
out=_httpin;
run;
proc import file=_httpin out=test dbms=xlsx replace;
run;
If you want the excel file saved somewhere (sounds like you don't, but if you do), then instead of temp assign that httpin filename to a real location.
filename _httpin "c:\temp\whatever.xlsx";
If you really want to do that binary file copy business, do it this way:
filename _httpin temp;
filename _httpout "c:\temp\Excel Precision.xlsx";
proc http method="get"
url="https://github.com/snoopy369/SASL/raw/master/Excel%20Precision.xlsx"
out=_httpin;
run;
data _null_;
_in = fopen('_httpin','I',1,'B');
_out= fopen('_httpout','O',1,'B');
rec='20'x;
do while (Fread(_in) eq 0);
rc = fget(_in,rec,1);
rc = fput(_out,rec);
rc = fwrite(_out);
end;
rc = fclose(_in);
rc = fclose(_out);
run;
proc import file=_httpout out=test dbms=xlsx replace;
run;
I am trying to read numeric data from the file. But I am to read it properly the output that I am getting from my SAS program is attached. I recently started learning SAS programming.
I am using SAS University Edition on the windows machine. I already tried by reading data in character as well as numeric formate.
data ds;
infile '/folders/myshortcuts/my_folder/exrate.sas7bdat';
input s ;
run;
I am expecting the same table to be as an output result.
Data File
Output
There are 3 things you need to do:
Make sure that the folder you put the dataset in is accessible to the SAS University Edition VM. Did you follow the setup guide in full? Usually that directs you to set up a folder that becomes available within the VM as /folders/myfolders
Assign a library pointing to the folder using a libname statement.
Use a set statement to access the dataset, not an infile statement. The latter is for reading raw data like csv files.
The sas code should look like this:
libname mylib "/folders/myfolders"; /*Change this to point to your folder path if you're sure you've got the right one*/
data ds;
set mylib.exrate(keep = s);
run;
I've scoured the internet but cannot seem to figure this out. My question is, if I have a sas7bdat file, how can I read a sas7bdat file in SAS studio so that I can work with it.
I've tried:
libname test 'C:\Users\name\Downloads\test.sas7bdat';
which gives me the error that library test does not exist and if I try the following, I know that I need an INPUT which I don't know of unless I can see into the file.
DATA test;
INFILE 'C:\Users\lees162\Downloads\test.sas7bdat';
RUN;
Is there something I'm missing?
Libref's that you create via the LIBNAME statement point to directories, not individual files.
libname test 'C:\Users\name\Downloads\';
INFILE is for reading raw data files. To reference an existing SAS dataset you use a SET statement (or MERGE,MODIFY,UPDATE statement).
set test.test ;
Note that you can skip defining a libref and just use the quoted physical name in the SET statement.
DATA test;
set 'C:\Users\lees162\Downloads\test.sas7bdat';
RUN;
Of course to use C:\ in the paths this is assuming that you are using SAS/Studio to point to full SAS running on your PC. If you are using SAS University Edition then it is running in a virtual machine and you will need to put the SAS dataset into a folder that is mapped to the virtual machine and then reference it in the SAS code with the name that the virtual machine uses for the directory.
So something like:
DATA test;
set '/folders/myfolders/test.sas7bdat';
RUN;
Libname is just pointing the location and once you have done that you can use that libname followed period and dataset in your set statement
libname test "C:\Users\name\Downloads";
DATA test;
set test.asl;
RUN;
One possible reason could be that you are using the SAS University edition (It doesn't support variable library address).
From one of the SAS community Q/A:
"When you are using the SAS University Edition, any libraries that you create must be assigned to a shared folder. You access your shared folder with this pathname: /folders/myfolders/. Always use '/' in the directory path, even in Windows operating environments"
After setting the directory address, proceed as instructed by Tom above in one of the answers.
Suppose you have the sas dataset at location. C:\Users\name\Downloads\test.sas7bdat
libname download 'C:\Users\name\Downloads';
proc sql;
select * from downloads.test;
run;
you can read your dataset like a table using the proc sql, in case you want to query the dataset, but if you want to modify the existing dataset then you can use the data setp as mentioned by #krian.
I am using SAS Studio(completely browser based). I need to export a dataset to my local machine in the .sas7bdat file format. I think it should be something like PROC EXPORT data = sqrtReg2 outfile = "C:\Documents\SAS\Target_Wins.sas7bdat";. But that returns the error ERROR: Unable to determine datasource type. Please use the DBMS= option.. But the DBMS option only allows for CSV, tab and DLM. How do I export this data set to my local machine in the .sas7bdat file format?
With the SAS University Edition you can setup shared folders in the virtual machine where SAS runs that are mapped to actual folders on your real machine.
For example you might have mapped C:\Documents\SAS\ to /folders/myfolders. You cannot write to other locations on your real machine that are not mapped so that the virtual machine can see them. Check the documentation for exact details of getting the folders mapped.
The normal way to have SAS place a dataset then is to create a libref that points to the folder and then use a two level name when referencing the data set. You could create a libref named OUT for example:
libname out '/folders/myfolders/';
data out.target_wins;
set sqrtReg2;
run;
But you can also just refer to the file directly without first creating a libref.
data '/folders/myfolders/target_wins';
set sqrtReg2;
run;
Note that since SAS is actually running in Unix you cannot use CamelCaseFileNames for your SAS datasets. The files will always be in all lowercase letters.
None of the answers worked for me. Maybe because after April 2021, they have made changes to the platform(University). So, after a lot a time searching, I found what I needed.
You can easily export the sas dataset to csv, xslx, by just right clicking on the dataset and selecting export as csv, xlsx, etc.
For exporting to sas7bdat file, do:
Create your dataset, I am creating from csv, so create a program1(.sas) to first convert csv to sas dataset.
proc import file="/home/u123/mydata.csv"
out=work.mydata
dbms=csv
replace;
run;
This will create your sas dataset.
IMP Go to "Libraries" at the right bottom, and hit "My Libraries" -> New Library -> Name it(eg - test), give path(eg - /home/u123/sasuser.v94)
Check library creation, and HIT "Refresh Library Session" on right pane, don't refresh the page.
Now create a separate program2(.sas), to export the dataset to .sas7dbat file.
PROC COPY IN=WORK OUT=test;
SELECT mydata;
run;
quit
This will create a .sas7bdat file in your directory, with same name as your dataset.
These exact steps worked out for me.
It won't work for 2 reasons.
You can't export a SAS dataset to a SAS dataset (.sas7bdat) - Proc Export will export to excel, csv, etc but not to a .sas7bdat.
you're running SAS Studio from within a Virtual Machine that uses Linux as OS so path to create an external file is incorrect. You haven't hit this error but you will once you use the right filetype.
When you installed SAS Studio you should have created a shared folder. This folder is accessed from within SAS Studio as /folders/myfolders/filename.
So your code should looks like this:
PROC EXPORT data = sqrtReg2
outfile = "/folders/myfolders/Target_Wins.csv";
run;
From Windows the path to this shared folder will depend upon where you installed your VM.
Option 2
If what you need is the dataset then try the following code:
libname out "/folders/myfolders/";
proc copy in=work out=out;
select sqrtReg2 ;
run;
Again, table will be in your shared folder which is accesible from Windows.
I used just like option 2 in the above answer. I need to export my polygon data from SAS Studio in my virtual machine so that i can import it to my Visual Analytics. and it worked just fine.
libname out "/folders/myfolders/";
proc copy in=work out=out;
select my_map;
run;