Read a sas7bdat file in SAS Studio - sas

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.

Related

How to copy data afrer "cards"/"datalines" in SAS

I have to perform statistical analysis on a file with hundreds of observations and 7 variables(columns)on SAS. I know that it is necessary to insert all the observations after "cards" or "datalines". But I can't write them all obviously. How can I do? Moreover, the given data file already is .sas7bdat.
Then, since (in my case) the multiple correspondence analysis requires only six of the seven variables, does this affect what I have to write in INPUT or/and in CARDS?
You only use CARDS when you're trying to manually write a data set. If you already have a SAS data set (sas7bdat) you can usually use that directly (there are some exceptions but likely don't apply here).
First create a libname to the folder where the file is:
libname myFiles 'path to fodler with sas file';
Then load it into your work library - this is a temporary space that is cleaned up when you're done so no files here are saved permanently.
This copies it over to that library - which is often faster.
data myFileName;
set myFiles.myFileName;
run;
You can just work with the file from that library by referencing it as myFiles.myFileName in your code.
proc means data=myFiles.myFileName;
run;
This should get you started, but you should take the SAS free e-course to understand the basics, it will save you time overall.
Just tell SAS to use the dataset. INPUT statement (and CARDS/DATALINES or INFILE statement) are for reading from text files.
proc corresp data='/my directory/mydataset.sas7bdat' .... ;
...
run;
You could also make a libref that points to the directory and use two level name to reference the dataset.
libname myfiles '/my directory/';
proc corresp data=myfiles.mydataset .... ;
...
run;

How to read data from data file using infile statement?

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;

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

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;

SAS File <lib>.<dataset>.DATA does not exist but proc datasets shows dataset

I'm trying to move a SAS dataset over to our Linux server from a client. They created it on SAS 9.4, 64-bit on Windows 7. I'm using SAS 9.4, 64-bit on Linux.
If I do
proc datasets library=din;
run;
I get the following in my log
Libref DIN
Engine V9
Physical Name /sasUsr/DM/DATA/SAS_DATA/201510_SSI
Filename /sasUsr/DM/DATA/SAS_DATA/201510_SSI
Inode Number 46358529
Access Permission rwxrwxr-x
Owner Name cvandenb
File Size (bytes) 4096
Member File
# Name Type Size Last Modified
1 SAMPLE_FROM_SSI DATA 131072 09/14/2015 17:07:01
2 TEST DATA 131072 09/15/2015 09:35:59
15 run;
but when I do
data test;
set din.sample_from_SSI;
run;
I get
18 data test;
19 set din.sample_from_SSI;
ERROR: File DIN.SAMPLE_FROM_SSI.DATA does not exist.
20 run;
I also created a dummy dataset din.test and was able to proc print it. This seems to either be a version compatibility issue or transmission issue. I thought this would be straightforward. Any suggestions? I'm moving the file from windows to Linux with WinSCP. I'd rather not have to request a .csv and create the input statement, but will if I have to. Your help is appreciated.
Thanks,
Cory
If you are talking about an actual SAS dataset then make sure that the name of the file is in all lowercase letters and has the extension of .sas7bdat. If the source file from Windows did not have an extension of .sas7bdat then perhaps you are not dealing with a SAS dataset, but some other type of file.
In SAS code it does not matter whether you reference a dataset using upper or lower case letters. So you can reference a datasets as sample_from_SSI or Sample_From_Ssi to refer to the same file. The same is true of general filenames on a Windows machine. But on Unix system file names with different use of upper and lower case letters are distinct files. SAS requires that the filename of a SAS dataset must be in all lowercase letters.
So if you write:
libname DIN '/sasUsr/DM/DATA/SAS_DATA/201510_SSI';
proc print data=DIN.SAMPLE_FROM_SSI;
run;
Then you are looking to make a listing of the data in a file named:
/sasUsr/DM/DATA/SAS_DATA/201510_SSI/sample_from_ssi.sas7bdat
I usually get a note about CEDA in this case not missing data.
Create either a CPORT or XPORT file using the associated proc, PROC CPORT or XPORT and then move that file.
Try referring to the data with all caps as well, which I don't think should be the issue, but is possible.
I would try using PROC COPY directly on the libname, as you can select memtype=data that way without explicitly specifying the file.
If SAS still can't do that, then you might have a permissions issue or something else that is outside of the SAS realm I suspect.
Try using PROC CPORT and PROC CIMPORT.
Use the CPORT Procedure to convert the file into a transport file.
Use the CIMPORT Procedure to convert the transport file to a SAS format.
There is an example that sounds similar to what you are doing here.
According to SAS, the general procedure is:
A transport file is created at the source computer using PROC CPORT.
The transport file is transferred from the source computer to the target computer via communications software or a magnetic medium
The transport file is read at the target computer using PROC CIMPORT.
Note: Transport files that are created using PROC CPORT are not
interchangeable with transport files that are created using the XPORT
engine.
If that doesn't work, or it is taking a very long time to figure out, it would be faster to ask them for a CSV and import it directly using PROC IMPORT. It should read in quite easily, especially if it comes from PROC EXPORT.