DATA Step, Macro, Functions and more

Writing out a SAS Dataset ?

New Contributor
Posts: 2

Writing out a SAS Dataset ?

Starting with PC-SAS and have been giving a SAS dataset  ccc.sas7bdat.


On that dataset is a variable that I have modified, I have made the new variable and dropped the previous variable.


How do I then get the new SAS dataset written back to the LIBNAME:


My current code:


libname wombat 'd:\rawdata';

data wombat.sra1 (drop = ssan); set one;

proc contents data=wombat.sra1;

data _null_; set wombat.sra1; file 'd:\rawdata\ddd.sas7bdat'; 

Posts: 9,374

Re: Writing out a SAS Dataset ?

Posted in reply to SSegBeaverBoy
It is not clear from your example what you are trying to do. In your first DATA step program you are creating the SAS dataset SRA1 in the "WOMBAT" library location of d:\rawdata. WOMBAT.SRA1 is being created FROM the SAS dataset WORK.ONE.

Since you do not show how WORK.ONE is created, there must be a step missing in your current code. Where does WORK.ONE come from?

Next, it appears that WOMBAT.SRA1 will have all the same variable as WORK.ONE except that you will NOT have the variable SSAN in WOMBAT.SRA1 and you WILL have the variable SSAN in WORK.ONE.

You then run a PROC CONTENTS to see the information in the SAS dataset WOMBAT.SRA1.

It is your 3rd step that I find confusing. DATA _NULL_ generally tells SAS that you need to use the DATA step programming language, but you do NOT want to create a SAS dataset with the program. Your input to this program is the dataset, WOMBAT.SRA1 that you just created.

Here's the confusing part -- the use of a SAS dataset name in your FILE statement is completely confusing. In your original post, you refer to getting a file called ccc.sas7bdat and yet, nowhere in your program steps do you refer to ccc.sas7bdat. So that is one discrepancy between your statement of the problem and the code you've shown.

However, with a FILE statement in a DATA _NULL_ program, typically, you are either writing to FILE PRINT or FILE PRINT ODS or you are creating an ASCII text file usually with PUT statements to write your report or with PUT statements to create your ASCII text file.

A SAS dataset (such as you reference by the full name in your FILE statement) is a proprietary type of file. I would expect that you are seeing this note in your LOG:
NOTE: 0 records were written to the file 'd:\rawdata\ddd.sas7bdat'.

So even though it appears that the program works (because you will not see any ERROR messages), the fact that 0 records are created should be a warning that something is not happening the way you want. If you want to CREATE a dataset named ddd.sas7bdat in the d:\rawdata file location, then you need to do something like this:
data wombat.ddd;
set wombat.sra1;

proc contents data=wombat.ddd;

Then, when you look at the PROC CONTENTS output, you should see that the location of ddd.sas7bdat is in the d:\rawdata folder (assuming that is where you want it).

New Contributor
Posts: 2

Re: Writing out a SAS Dataset ?

Posted in reply to Cynthia_sas

Thank you -- as a mainframe programmer of over 25 years, it has been too long understanding libraries and how to properly write out a file -- your code helped me realize that I needed to just use the libname and membernames of the dataset I was creating to get the job done.  I got it to work so I thank you !  Scott S

Super User
Posts: 23,791

Re: Writing out a SAS Dataset ?

Posted in reply to SSegBeaverBoy

Your code should be something like below. Please read the comments.


*Create library to get/put data;
libname wombat 'd:\rawdata';

*bring dataset into work library from wombat library;
data one (drop = ssan);  /*new dataset to be created*/
set wombat.sra1; /*Dataset being read from*/

new_var=ssan*2; /*create new var*/

*examine contents of dataset one;
proc contents data=one;

*write back to library;
data wombat.sra2; /*new dataset, back in wombat library*/
set one; /*dataset being read from*/
Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation