BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GeorgeSAS
Lapis Lazuli | Level 10

Hello all;

 

I want use data step generate external file. the put statment does not work(need to keep the enter between rows) and need to be revised. please help.(I don't want add "put" in each line, because the real program has hundreds lines of codes)

 

Thank you!

 

 

 

Filename  need  "c:\temp\newcode.sas";
data _null_;
file need;
*------------ the code below need to be print to external file "newcode.sas*"------------------;
put 
"
/*Program name  newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
";
*-----------------------end -------------------------------;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
GeorgeSAS
Lapis Lazuli | Level 10

*----------------C:\temp\test\aa.txt--------------------------------;
/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;

*----------------------------------------------;

 

data textdata;
infile "C:\temp\test\aa.txt" truncover;
input textline $100.;
run;
data textdata2;
length z $200;
set textdata;
z=cat("put '",strip(textline),"';");
run;
data textdata3;
set textdata2;
file "C:\temp\test\aa2.txt";
put z;
run;
data _null_;
file 'C:\temp\test\aa3.sas';
%include "C:\temp\test\aa2.txt";
run;

 

Thank you!

 

 

Also thank Tom's good code,that works better:

data _null_;
file "c:\temp\newcode72.sas" ;
input;
put _infile_;
cards4;
/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;
;;;;

View solution in original post

10 REPLIES 10
Reeza
Super User

Have you looked into PROC PRINTTO?

 

You need to put the text in quotes to output it. 

 

Astounding
PROC Star

As you've seen, each PUT statement writes to one line (at least, by default).  That gives you one possible solution:  multiple PUT statements:

put "/*Program name  newcode.sas*/";
put "/*This SAS program will print sashelp.class*/";
put "proc print data=sashelp.class;";
put "run;";

 

Not elegant, but at least it works.  Alternatively, a slash in a PUT statements tells SAS to move to the next line.  That means a single PUT statement can write out multiple pieces of text, to multiple lines, in this fashion:

 

put "/*Program name  newcode.sas*/"
/ "/*This SAS program will print sashelp.class*/"
/ "proc print data=sashelp.class;"
/ "run;"
;
ballardw
Super User

Are you just trying to write out to a text file the SAS program code you want?

One put statement will only generate one end of line unless you embedd the appropriate character(s) as a variable

Try:

data _null_;
file need;
*------------ the code below need to be print to external file "newcode.sas*"------------------;
put "/*Program name  newcode.sas*/";
put "/*This SAS program will print sashelp.class*/";
put "proc print data=sashelp.class;";
put "run;";
*-----------------------end -------------------------------;
run;
Reeza
Super User

http://www2.sas.com/proceedings/forum2007/037-2007.pdf

 

My guess is you're trying to accomplish something similar to this.

art297
Opal | Level 21

Given your example, what do you want the result to look like?

 

Art, CEO, AnalystFinder.com

 

GeorgeSAS
Lapis Lazuli | Level 10

I want to create a new SAS program "c:\temp\newcode.sas" The full codes inside the file is:

/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;

My question is I can use data _null_ step to create the file? Thanks!

art297
Opal | Level 21
data _null_;
  file 'c:\temp\newcode.sas';
  put ' /*Program name newcode.sas*/';
  put '/*This SAS program will print sashelp.class*/';
  put 'proc print data=sashelp.class;';
  put 'run;';
  put 'data a;';
  put 'set sashelp.class;';
  put 'run;';
run;

Art, CEO, AnalystFinder.com

Tom
Super User Tom
Super User

As long as you are not running in a macro and the program code you want to copy does not include four semi-colons then you can use inline data.

 

data _null_;
  file "c:\temp\newcode.sas" ;
  input;
  put _infile_;
cards4;
/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;
;;;;
LinusH
Tourmaline | Level 20
Just out of curiosity, since there have some similar posts. What is the requirement / overall solution / process that makes you want to this (way)?
Data never sleeps
GeorgeSAS
Lapis Lazuli | Level 10

*----------------C:\temp\test\aa.txt--------------------------------;
/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;

*----------------------------------------------;

 

data textdata;
infile "C:\temp\test\aa.txt" truncover;
input textline $100.;
run;
data textdata2;
length z $200;
set textdata;
z=cat("put '",strip(textline),"';");
run;
data textdata3;
set textdata2;
file "C:\temp\test\aa2.txt";
put z;
run;
data _null_;
file 'C:\temp\test\aa3.sas';
%include "C:\temp\test\aa2.txt";
run;

 

Thank you!

 

 

Also thank Tom's good code,that works better:

data _null_;
file "c:\temp\newcode72.sas" ;
input;
put _infile_;
cards4;
/*Program name newcode.sas*/
/*This SAS program will print sashelp.class*/
proc print data=sashelp.class;
run;
data a;
set sashelp.class;
run;
;;;;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 892 views
  • 2 likes
  • 7 in conversation