Setting working directory in SAS - 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

Related

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.

How can I create a SAS library using existing SAS files?

I have a directory containing a set of SAS scripts, data files and also csv files. I want to now associate this directory with a library in SAS. I've created a library with the path name to the directory but the contents of the library are empty when I look via SAS but not via Windows Explorer.
How can I create a SAS library with all of these existing files inside it?
Using Windows 8 SAS 9.3
PS. New to SAS hence what is possibly a very easy question.
A SAS library can contain objects such as SAS tables, SAS views, SAS catalogs (only SAS can read this catalog, on the operating system it's just a file).
In SAS catalog you can write e.g. SAS formats, SAS macros.
To assign a library use a libname statment.
libname libref base "path_to_OS_catalog";
If you want to read a file using SAS e.g. CSV, TXT files, use a filename statment. Check a documentation for examples.
filename fileref "path_to_file";
In other words, it's correct that your library is empty, because in a OS catalog there are no SAS objects.

Converting Excel file into SAS Dataset

I'm trying to import a dataset into SAS enterprise guide, however am having trouble 'converting' my excel file into a new SAS dataset file. Any easy way to do this?
Like this:
libname LIBRARYNAME 'filesource...'
data Project2
set exdata.Project;
<math>
run;
But the file I have the data in is a Excel file... and it can't find it
You are close, if you have to ability to access local files something like this may work. You may need to play around with the sheet name. Open the library once it's assigned to see how SAS refers to the sheets.
Libname mylib XLSX 'path to XLSX file';
Data want;
Set mylib.sheet_name;
Run;
Libname mylib;
Otherwise if your using EG on a server and your excel file is local you can import using the GUI tool.
In Enterprise Guide, you can create an import step. This is preferable for EG projects that will always be in EG, as it is OS-agnostic, server-agnostic, and version-agnostic, and leaves a single step on the process flow that you can move around where you need it to be run.
Choose (File menu) -> (Import Data), and then it will guide you through importing the file. When it completes, this will leave on your process flow an Import Data step that you can link to other programs (if you're using this program repeatably).
During that wizard, you will tell it what dataset name you want it saved as, and that will then exist as a SAS dataset in your project (usually in the work library unless you specify otherwise).

Export .sas7bdat from SAS Studio to local machine

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;