data _null_ put "embed SAS code" in data step

Accepted Solution Solved
Reply
Regular Contributor
Posts: 236
Accepted Solution

data _null_ put "embed SAS code" in data step

[ Edited ]

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;

 


Accepted Solutions
Solution
‎05-15-2017 12:01 PM
Regular Contributor
Posts: 236

Re: data _null_ put "embed SAS code" in data step

[ Edited ]

*----------------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


All Replies
Super User
Posts: 19,093

Re: data _null_ put "embed SAS code" in data step

Have you looked into PROC PRINTTO?

 

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

 

Super User
Posts: 5,362

Re: data _null_ put "embed SAS code" in data step

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;"
;
Super User
Posts: 11,121

Re: data _null_ put "embed SAS code" in data step

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;
Super User
Posts: 19,093

Re: data _null_ put "embed SAS code" in data step

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

 

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

PROC Star
Posts: 7,433

Re: data _null_ put "embed SAS code" in data step

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

 

Art, CEO, AnalystFinder.com

 

Regular Contributor
Posts: 236

Re: data _null_ put "embed SAS code" in data step

[ Edited ]

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!

PROC Star
Posts: 7,433

Re: data _null_ put "embed SAS code" in data step

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

Super User
Super User
Posts: 6,844

Re: data _null_ put "embed SAS code" in data step

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;
;;;;
Super User
Posts: 5,383

Re: data _null_ put "embed SAS code" in data step

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
Solution
‎05-15-2017 12:01 PM
Regular Contributor
Posts: 236

Re: data _null_ put "embed SAS code" in data step

[ Edited ]

*----------------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;
;;;;

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 207 views
  • 2 likes
  • 7 in conversation