DATA Step, Macro, Functions and more

9.4 Proc Export to txt without REPLACE

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

9.4 Proc Export to txt without REPLACE

Hi folks,

 

My objective is to create a txt file from a SAS dataset with both the variable names and the data seperated by "^".

 

My initial strategy was to divide it into two parts: first export a file with only the variable names. Then export the data to the same txt file. 

*STEP 1;

data _null_;

set work.mySASdata;

Retain count 0;count+1;

file "&filelocation";

length line $ 200;

varnameline=strip('var1^var2^var3^var4');

if count=1 then put VarnameLine;

run;

 

*STEP 2;

proc export data=mySASdata

outfile="&filelocation"

dbms=dlm replace;

delimiter='^';

putnames=no;

run;

 

The problem is that the second step seems to require the option REPLACE, which would overwrite, rather than append to, the file with only the variable names seperated by ^.  

Q1: Is there a way to export SAS9.4 to text without replacement?

Q2: Is there a way to do this in one export or datastep so I won't need an answer to Q1 above?

 

Thanks,

Tim

 

 

 

 


Accepted Solutions
Solution
‎09-28-2017 03:17 PM
Frequent Contributor
Posts: 149

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to TimWright

Why do you need two steps? The following proc export seems to produce the desired result:

proc export data=sashelp.class dbms=dlm file="&filelocation" replace;
   delimiter='^';
run;

If you don't want/have to print the real variable names, you have two options:

  1. Use one data-null-step.
  2. Create a view renaming the variables, then use proc export.
data _null_;
   set sashelp.class;
   file "&filelocation" delimiter="^";
   
   if _n_ = 1 then do;
      put "Var1^Var2^Var3^Var4^Var5";
   end;
   
   put Name Sex Age Weight Height;
run;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to TimWright

It sounds like you are want to append more than one data set to the exported file.

If that is the case then you might have to go back to a data _null_ for the follow up data and use the FILE statement option MOD which writes at the end of an existing file if it already exists.

 

Or combine all of the data involved into a single data set and use Proc Export with putnames=yes

Occasional Contributor
Posts: 11

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to TimWright

It occurs to me that one 3-step solution would be the following:

STEP1: export SAS data to excel 

STEP2: import data from the excel, starting with line 1 (rather than two).The new SAS file, work.NewFile, should have the original variable names as the first record of the data.

STEP3:

Proc Export data=work.NewFile 

outfile='c:\newTxtFile.txt'

dbms=dlm replace ;

delimiter='^';

Putnames=No;

run;

 

 

Solution
‎09-28-2017 03:17 PM
Frequent Contributor
Posts: 149

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to TimWright

Why do you need two steps? The following proc export seems to produce the desired result:

proc export data=sashelp.class dbms=dlm file="&filelocation" replace;
   delimiter='^';
run;

If you don't want/have to print the real variable names, you have two options:

  1. Use one data-null-step.
  2. Create a view renaming the variables, then use proc export.
data _null_;
   set sashelp.class;
   file "&filelocation" delimiter="^";
   
   if _n_ = 1 then do;
      put "Var1^Var2^Var3^Var4^Var5";
   end;
   
   put Name Sex Age Weight Height;
run;
Occasional Contributor
Posts: 11

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to error_prone

Wy do I need two (or three) steps? Because I'm a SAS dummy

 

 

Occasional Contributor
Posts: 11

Re: 9.4 Proc Export to txt without REPLACE

Posted in reply to error_prone

I cannot believe that I spent so much time trying to figure out an alternative solution based on the assumption that it would export the data in the wrong way, without even testing it.

 

UGH!

 

Thanks!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 139 views
  • 1 like
  • 3 in conversation