A short simple tutorial on SAS libname statements for SAS for Windows Michael Oakes UMN Epidemiology September 2008
The libname statement creates a library reference (libref) for a SAS program. A libref is a pointer or short-cut to a place on your computer hard-drive where data and programs and other computer files are stored and retrieved. A libref is like your home street address where mail is sent from and delivered to. In general, the basic syntax for a libname is libname name 'path'; Specifically, for my computer, it might look like this libname thesis d:\data\thesis';
Look, SAS needs to read data from someplace and write results to someplace. Where is this someplace? On your computer s hard-drive! Your computer hard-drive is like a town. There are lots of addresses and places where people live. Each town (ie, hard-drive) is a bit different. How does the postman know where to deliver your mail? A street address that s specific or particular to your town. But once you know it, you re all set. Just address your letter and it ll be delivered. In SAS, your data s street address is called a path.
Your computer s hard-drive is nothing more than a metallic disk that holds electronic files. It spins around fast like a music CD/DVD. The disk is organized some stuff here, some there. Some drives are messy, others are well organized.
It s easiest to think about your hard-drives organization as a outline. Some major groups, some minor or sub groups. Let s call groups folders. Thus, the stuff on your hard-drive organized into a bunch of major and sub folders.
We typically use Windows File Explorer to examine and/or make folders.
You often navigate to a particular folder which is specified by a path. Path Folder File (SAS) in Folder
OK so let s get SAS to read and write data files to this folder/directory It s easy! Just tell SAS the path Conceptually, we d like to say to SAS: Hey, read from and write data to 124 Main Street In fact, we tell SAS the path where (data) files are located Simply type the path into your program Just make sure the path you type actually exists on your hard-drive.
Path some actual place on your hard-drive Libref This is nothing more than some name a short-cut that will now be used to refer to the path it saves you from typing the path each and every time you wish to refer to it
The libref thesis refers to the path Once submitted to SAS, you only need to use the libref (ie, thesis) instead of the path it s a short-cut!
Path names do not include the file name! The path does not include the file name The path is like an address to a house. It does not tell you what s in the house This is the filename; the file you wish to read (ie, set) The file must exist in the folder specified by the libref In this case, the file does exist in the folder specified by the libref (ie, path)
The Libref (ie, path short-cut) is used to read/write files These match. Thus, SAS will look for the data set named my_data.sas7bdat in the D:\Data\Thesis folder
Here I specify two librefs One a referring to one path, another b to another path. You can specific as many as you wish!
To summarize: 1. Libname statements are written in SAS programs to tell SAS where to read/write data; they are nothing more than programming short-cuts. 2. The actual place where data is written is some folder on your hard-drive; each folder has an address, which we call a path. 3. Each short-cut has a name, called a library reference or libref. 4. The libref name can be anything really but keep it short and sweet; most folks use a or b or thesis or something like that. 5. Once one defines a libref in their program, they submit the program. This initializes or assigns the libref. After this step, you re good to go!
Let s do this First, decide where you d like data to written/read from. I use Windows File Explorer for this step.
So I ll now type this path into my SAS program
This line tells SAS to associate the libref thesis with the path This line tells SAS to write a dataset called analysis to the temp or default work. libref This line tells SAS to read a dataset called my_data from the thesis. libref
No libref? What s up?!!!? When no libref is written, SAS assumes you re refering to the temporary (ie, work.) libref The work. folder is just a place on your hard-drive. It s hidden from view, but the only difference with it is that the contents (files etc) are deleted after you close/exit SAS.