BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

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

My97_0-1612811461568.png

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

 

Phil_NZ
Barite | Level 11

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?

 
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Tom
Super User Tom
Super User

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;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1650 views
  • 2 likes
  • 2 in conversation