How can I store data permanently in SAS using a Libname?

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Data
Click For Summary

Discussion Overview

The discussion centers around the process of storing data permanently in SAS using a Libname statement, particularly in the context of the University Version of SAS running in a remote Virtual Directory within SAS Studio. Participants explore the syntax and methods for importing data, creating permanent datasets, and managing storage effectively.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant outlines a method for importing data into SAS temporarily using the Work directory, detailing the steps for reading a file and the implications of temporary storage.
  • Another participant critiques the initial approach, suggesting that the Libname statement is correct but emphasizes the need to create a new dataset in the target Libname for permanent storage, providing an example of the syntax required.
  • There is a discussion about the use of SAS's built-in compression for datasets, with one participant noting that it is beneficial for wide datasets and explaining how to implement it.
  • Several participants discuss the ease of working with data in SAS once it is imported, with mixed opinions on the design of the language and its learning curve.
  • One participant expresses a desire for centralized references or resources for learning SAS, highlighting the fragmented nature of available information.
  • Another participant shares their experience of learning SAS on the job and discusses the importance of managing permanent storage carefully to avoid accidental data loss.

Areas of Agreement / Disagreement

Participants generally agree on the importance of using the Libname statement for permanent storage, but there are differing views on the specifics of syntax and best practices. The discussion remains unresolved regarding the best centralized resources for learning SAS.

Contextual Notes

Participants mention the complexity of the INPUT syntax and the need for careful management of permanent storage, indicating that there may be nuances in the implementation that are not fully explored in the discussion.

Who May Find This Useful

Individuals learning SAS, particularly in academic settings, those looking to manage data storage effectively, and users seeking to understand the nuances of importing and exporting datasets in SAS.

WWGD
Science Advisor
Homework Helper
Messages
7,795
Reaction score
13,095
Hi All,
Trying to get basics of inputting data into SAS ( University Version, i.e. in remote Virtual Directory in SAS Studio . I hope my layout , nor my general writing are too confusing. Feel free to suggest a different format):
Please critique:
We want to read file 'FileName' into SAS for some processing
1) To input/import data when 'FileName' is _not_ in SAS in non-permanent form , i.e., to be stored in 'Work' directory and will disappear after session is over.:

--------------------------------------------
1.1) DATA [FileName1] ;( Original File name [FileName] modified nto , e.g. FileName1)
1.2) INFILE [Pathname for FileName];
1.3) INPUT [Field1] [Field2]...[Fieldn]; : These are the fields/columns from [FileName] we want to import
1.4) RUN;
Data will be read into temporary file within 'Works' folder in Virtual Drive. File will disappear once session is closed.
----------------------------------------------

2) To store data file [FileName] permanently in SAS in Folder [Folder]. We use a 'LibName which is an alias for the name for the folder where we want to store things together with the pathname for the folder.:

2.1) LIBNAME [Alias for FileName] [ Pointer for or Pathname for [Folder] ];
2.2) SET [FileName, from step 1.1];
2.3) RUN ;

Is this correct?
 
Technology news on Phys.org
1 - More or less. The syntax for INPUT is quite complex and let's you specify field widths and things like that. I'm not familiar with the details - just Google if what you try doesn't quite work.

2 - No. Your libname statement is about right, but then you need to create a copy of the dataset by creating a new dataset in the target libname. You do that by qualifying the dataset name with a libname. So having read in a dataset called myfile, copy it to permanent store with
Code:
* Make perm an alias for your permanent storage;
libname perm "/path/to/directory";
* Create a dataset called myfile in perm, containing a copy of myfile;
data perm.myfile;
    set myfile;
run;
If the dataset has many columns, it can be worth using SAS' builtin compression. This is completely transparent - SAS reads its compressed files without any extra effort on your part. But it compresses rows separately, so it's only worth doing if your dataset is wide.
Code:
* Make perm an alias for your permanent storage;
libname perm "/path/to/directory";
* Create a compressed dataset called myfile in perm, containing a copy of myfile;
data perm.myfile(compress=binary);
    set myfile;
run;
Unless you are doing a lot of writing to the permanent libname I recommend immediately de-assigning it and reassigning it read only.
Code:
libname perm clear;
libname perm "/path/to/directory" access=readonly;
This way it's harder to accidentally overwrite something in permanent storage.
 
Last edited:
  • Like
Likes   Reactions: WWGD
Thanks for the detailed comments.i s it at least easy to work with the data once you have it in the system?
 
Fairly straightforward, yes, although it's fairly obvious that the language just grew rather than being properly designed. Basically a data step reads in a data set line by line, executes the instructions you wrote, outputs the line, and repeats until it runs out of data.

Between data steps and the sort, summary and transpose procs you can usually massage the data into the form you want and then there are loads of analytical procs that do statistical tests and model fitting and stuff like that in a fairly simple way. You can also write SQL if you want with proc sql.

If you need a many-to-many join, use proc sql. Otherwise datasteps are usually faster. Always check the log file carefully (automated tools are you friend!) because SAS has some very odd ideas about what should be an error or warning and what only counts as a note.
 
Last edited:
  • Like
Likes   Reactions: WWGD
WWGD said:
Thanks for the detailed comments.i s it at least easy to work with the data once you have it in the system?
There is a learning curve to it, but it is a very good language for manipulating the data and generating reports.
 
  • Like
Likes   Reactions: WWGD
Does anyone know a good "centralized" reference? I have watched videos and read pages, but it is a bit here, a bit there. A problem too is that it is often too informal. I would like to know, if possible, e.g., when importing from the virtual (SAS Studio) drive, use this syntax.
When creating a file from scratch use... Anyone know any such source ( Including books, of course)?

EDIT: Also, sorry if this is a dumb question, but, does above suggestion on Libname apply to files in the virtual drive, one's PC drive, or does this not matter?
 
I'm afraid I learned on the job, updating code from a recently-departed SAS wizard with some input from my boss.
WWGD said:
EDIT: Also, sorry if this is a dumb question, but, does above suggestion on Libname apply to files in the virtual drive, one's PC drive, or does this not matter?
I use my permanent storage for things that take ages to generate, or the originals of some things. That is, things that are expensive (or impossible without sacrifices to the Gods of the IT Department) to recover if I accidentally PROC SORT NODUPKEY the wrong thing (which I have seen someone do). So I create permanent storage libnames with access=readonly to force a conscious choice to write to those folders. It's risk management. Don't bother if there's nothing you care about in a folder.
 
  • Like
Likes   Reactions: WWGD

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
3
Views
3K