DATA Step, Macro, Functions and more

Referencing library or external files using LIBNAME, FILENAME

Reply
Regular Learner
Posts: 1

Referencing library or external files using LIBNAME, FILENAME

Hi,

 

I have started into SAS Certification Prep Guide to prepare myself for SAS Base Certification. I always try to reference library once I start a new SAS session. (NOTE: I use SAS University Edition on my Windows system using Virtual Box and localhost). The problem occurs when I try to reference an external file using FILENAME statement.

Ex:

filename carss '/folders/myfolders/certprep/cardata.dat'

Question:

Does a file name carss should be already present in the library before running this command OR it is created on the fly?

I tried "work.carss" but it is no working as well.

I am really getting confused to referencing as I am using a virtual machine and not the actual environment. I guess it will be different for SAS Enterprise Guide.

 

Please clear my knowledge about referencing libraries and files in SAS University Edition.

Thanks a lot in advance.

Super User
Posts: 2,492

Re: Referencing library or external files using LIBNAME, FILENAME

Posted in reply to ronakagrawal
1- FILENAME does not reference libraries. LIBNAME does 2- The path to a library must exist (normally). The path to a file does not have to. Why do you say there is a problem? If it is because you try to use WORK.CARSS then a SAS table (not a file reference created with FILENAME) with that name must exist.
Super User
Posts: 13,887

Re: Referencing library or external files using LIBNAME, FILENAME

Posted in reply to ronakagrawal

CARSS as you defined it is a reference. If you do something that writes to the fileref then the content will be created, in most cases replacing the current content depending on how you send output.

 

If you want to read from the file, the referenced file will have to exist before attempting to read. Otherwise you will get a "file not found" error.

 

WORK.CARSS would refer to a SAS data set in the work library.

Your file reference created with the Filename statement is intended to tell SAS where to find external (i.e. typically non-SAS ) files.

 

You might use a data step to read that file and create SAS dataset with something similar to:

data work.carrs;

   infile carss <infile options>;

   <input statement>

;

or perhaps use an import wizard.

 

If "Carss" is supposed to be an existing SAS data set then you would use a LIBNAME statement to assign a library and point to the folder location such as

 

LIBNAME prep '/folders/myfolders/certprep';

If there are valid SAS data sets then they would be referenced such as PREP.Cars. However the file name you reference is likely not a SAS data set but perhaps a text file you are supposed to read for the exercises?

Super User
Posts: 23,951

Re: Referencing library or external files using LIBNAME, FILENAME

Posted in reply to ronakagrawal

ronakagrawal wrote:

 

I am really getting confused to referencing as I am using a virtual machine and not the actual environment. I guess it will be different for SAS Enterprise Guide.

 

 


EG will be similar to SAS Studio. Both are thin clients that are usually accessing SAS on a Server and you'll still have the same issues accessing files, so encountering this now isn't a bad thing. 

 

Basically, you need to first import the data once you have your file reference. The reference only creates a shortcut link to the file, which you then need to import it first, to create a SAS data set.

 

You can find tutorials on 

video.sas.com

 

Your question is likely most closely related to this video:

http://video.sas.com/detail/videos/how-to-tutorials/video/4573016758001/creating-a-sas-table-from-a-...

 

Specific answers to your question: 

Does a file name cars should be already present in the library before running this command OR it is created on the fly?

It does not need to exist, this is because filename references can also be used as output files. So if you were to export a data set to a text file, you could first create a file reference, but the file doesn't have to pre-exist. 

 

As someone else mentioned, FILENAME and LIBNAME statements are different. 

FILENAME references a file. 

A LIBNAME references a folder, usually with SAS data sets.

It can also reference files, specifically files such as XLSX or a database. 

 

 

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 73 views
  • 0 likes
  • 4 in conversation