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;
*----------------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;
;;;;
Have you looked into PROC PRINTTO?
You need to put the text in quotes to output it.
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;"
;
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;
http://www2.sas.com/proceedings/forum2007/037-2007.pdf
My guess is you're trying to accomplish something similar to this.
Given your example, what do you want the result to look like?
Art, CEO, AnalystFinder.com
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!
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
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;
;;;;
*----------------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;
;;;;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.