Libname with libref: how to access to a data source (library) - sas

I need to access a dataset into a library (/data/....) and create a temporary dataset/table to not overwrite the original one.
What I tried is to use the libname with libref:
libname libref '/data/....';
and create a temporary file using a data step:
set original;
run;
But something does not work, because I have not access to the folder dataset. Do I need to specify something else to the libname?
The path that I have is /data/folder/, without any information about the directory (example C:). Should I specify it?
Many thanks for your help.

libname mylib "/data/...";
data copy;
set mylib.original;
run;

Related

SAS xlsx libname engine on Viya 4

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.

ERROR: Physical file does not exist {on SAS studio (Academic ondemand-web based)}

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.

SAS Library changing

I'd like to change library name (a lot tables are assigned to this library). Is it possible to just only 'edit' library name to the new one or is it necessary to create a new library and then moved the tables to it?
Will the tables not be damaged when editing the name of an existing library?
LIBNAME Statement
Associates or disassociates a SAS library with a libref (a shortcut name), clears one or all librefs, lists the
characteristics of a SAS library, concatenates SAS libraries, or
concatenates SAS catalogs.
A LIBRARY is a place in which data sets can be found. The place can be a folder, a json file, an xml file, a remote database or any of numerous others.
A LIBREF is a reference to such a place.
The LIBNAME statement is used to create a LIBREF and provide any options needed by the library engine that mediates the access to the data sets.
You can have multiple librefs pointing to the same library
LIBNAME zoinks 'c:\projects\x\sasdata';
LIBNAME sweets 'c:\projects\x\sasdata';
You can also have a libref point to more than one place using concatenation
Example:
Suppose some company stores data sets in separate folders according to year and quarter, but you want access to them all through one libref.
LIBNAME INS2020
( 'c:\insurance\2020Q1'
'c:\insurance\2020Q2'
'c:\insurance\2020Q3'
'c:\insurance\2020Q$'
);
A libref is a moniker for data access. Changing (refactoring) the libref is akin to giving someone (or someplace) a better nickname than what was used in the past.
Libraries are pointers to files/locations. You can change the name without impacting any files within the library. You can also have multiple libraries pointing to the same location, though SAS will warn you if you do that.
libname demo '/folders/myfolders/';
*place file in demo;
proc copy in=sashelp out=demo;
select class;
run;quit;
*clear demo;
libname demo;
*assign new library to same location;
libname myFiles '/folders/myfolders/';
*check items;
proc datasets lib=myFiles;run;quit;

Read a sas7bdat file in SAS Studio

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.

Setting working directory in SAS

I would like to programmatically reallocate SAS work dir within a script. In other words, standard set up is ok, but, for some tasks, I would like to have some lines of code that change the default settings... Just for that session.
Thanks in advance for your attention
Use a library called user. When this library is present, sas defaults all datasets to the user library instead of the work library.
libname user '/folders/myfolders/proj1';
data want;
set sashelp.class;
run;
proc datasets library=user;
run;quit;
The User library enables you to read, create, and write to files in a SAS library other than Work without specifying a libref as part of the SAS filename. Once you associate the libref User with a SAS library, SAS stores any file with a one-level name in that library. Unlike the Work library, files stored in this library are not deleted by SAS when the session terminates.
http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n18m1vkqmeo4esn1moikt23zhp8s.htm