Hi all SAS Users,
Normally we assign the library as a folder containing the file we want to call. However, I did a search and see if there is any way to call the file directly.
And from this forum discussion , @Tom has said
So if you have a file name 'mydata.sas7bdat' (note that the filename MUST be in all lowercase letters as SAS is running in Unix environment where filenames are case sensitive) then you code could look like:
libname mylib '/folders/myfolders';
proc contents data=mylib.mydata; run;
or
proc contents data="/folders/myfolders/mydata.sas7bdat"; run;
I have two questions:
1. Is it strict that we must set the name of the sas7bdat in lowercase ?
2. Regarding how to access the file sas7bdat directly as mention above, when I run proc contents
proc contents data="/folders/myfolders/mydata.sas7bdat"; run;
SAS just showed me the information of the variables in this file, not the dataset, can you please tell me how to access the dataset directly without referring the directory?
Many thanks and warm regards.
P/S: I also try this code to access the file directly but SAS cannot import properly
data test;
infile "C:\Users\pnguyen\Desktop\New folder\winsorize.sas7bdat";
run;
output dataset "test" from SAS
You can use the same syntax with any other place where you would reference a dataset.
proc means data="C:\downloads\class"; run;
Notice that under the hood SAS will actually create a libref that points to that directory. Example:
proc contents data="C:\downloads\class" out=contents; run;
proc print data=contents;
var libname memname name type length ;
run;
Results:
Obs LIBNAME MEMNAME NAME TYPE LENGTH 1 WC000001 CLASS Age 1 8 2 WC000001 CLASS Height 1 8 3 WC000001 CLASS Name 2 8 4 WC000001 CLASS Sex 2 1 5 WC000001 CLASS Weight 1 8
Now once you have referenced the file a specific libref will exist and you could use it to reference the dataset instead as long as you are in the same SAS session.
proc means data=WC000001.class; run;
But if you prefer to use the normal libname.memname syntax then it is better to write your own LIBNAME statement so that you have control over the libref that is used as there is not really anyway to know in advance what libref will be generated by using the quoted physical filename syntax.
So use something like this instead:
libname mydata "C:\downloads" ;
proc contents data=mydata.class;
run;
proc means data=mydata.class;
run;
You cannot reference any file on a computer without specifying the directory. You could potentially change the current working directory for the process that is running SAS and then use a relative path instead of an absolute path:
x "cd data=""/folders/myfolders/mydata"" ";
proc contents data="mydata.sas7bdat" ...
but you are still specifying the directly implicitly by using a relative path.
Note you do not need to add the .sas7bdat extension in the quoted physical name. So if you used this:
data="/folders/myfolders/mydata"
then SAS will add the .sas7bdat extension when it goes to look for the file.
1. Is it strict that we must set the name of the sas7bdat in lowercase ?
If your SAS code is running on Unix then the while filename part of the path (not the directory part) must be in all lowercase. If the filename as has uppercase letters in it then SAS will just not find it since it will only look for the file with the lowercase letters in its name. If SAS is running on Windows then case does not matter as Windows does not allow two different files have names that differ only in the case of the letters in the name. So even if SAS did look for the lowercase filename Windows would return the one file with that name whether or not the name had any uppercase letters in it.
SAS just showed me the information of the variables in this file, not the dataset,
That comment makes no sense. The SAS dataset is the file. The file is the dataset.
data test; infile "C:\Users\pnguyen\Desktop\New folder\winsorize.sas7bdat"; run;
This dataset TEST does not have any variables nor any observations since you never defined any variables in the data step nor read in any data. The INFILE statement is for pointing at files that you want to read with an INPUT statement, but your data step does not have an INPUT statement. Even if it did have an INPUT statement it would not work as you probably want has a SAS dataset is not a text file to be read. It is complex binary structure. That is why you need to use the SET (or MERGE or UPDATE or MODIFY) statement to reference it so that SAS knows to interpret that binary file as a SAS dataset.
Hi @Tom !
Thank you for your explanation.
So, back to the one that I confuse you
@Tom wrote:SAS just showed me the information of the variables in this file, not the dataset,
That comment makes no sense. The SAS dataset is the file. The file is the dataset.
when I run this code
proc contents data="C:\Users\pnguyen\Desktop\New folder\winsorize.sas7bdat"; run;
SAS turns out a result about the information of winsorize dataset. My question is how we refer to this dataset later on, for example, a proc means afterward?
You can use the same syntax with any other place where you would reference a dataset.
proc means data="C:\downloads\class"; run;
Notice that under the hood SAS will actually create a libref that points to that directory. Example:
proc contents data="C:\downloads\class" out=contents; run;
proc print data=contents;
var libname memname name type length ;
run;
Results:
Obs LIBNAME MEMNAME NAME TYPE LENGTH 1 WC000001 CLASS Age 1 8 2 WC000001 CLASS Height 1 8 3 WC000001 CLASS Name 2 8 4 WC000001 CLASS Sex 2 1 5 WC000001 CLASS Weight 1 8
Now once you have referenced the file a specific libref will exist and you could use it to reference the dataset instead as long as you are in the same SAS session.
proc means data=WC000001.class; run;
But if you prefer to use the normal libname.memname syntax then it is better to write your own LIBNAME statement so that you have control over the libref that is used as there is not really anyway to know in advance what libref will be generated by using the quoted physical filename syntax.
So use something like this instead:
libname mydata "C:\downloads" ;
proc contents data=mydata.class;
run;
proc means data=mydata.class;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.