BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TimWright
Obsidian | Level 7

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

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
error_prone
Barite | Level 11

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

5 REPLIES 5
ballardw
Super User

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

TimWright
Obsidian | Level 7

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;

 

 

error_prone
Barite | Level 11

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;
TimWright
Obsidian | Level 7

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

 

 

TimWright
Obsidian | Level 7

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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