Define a macro that produces:
data _null_;
set books.list;
file "list2.txt" dlm='09'x;
put bookid:4. author:$30. isbn:isbn18. yearpub:comma6.,
run;
when called upon.
Very easy: just be sure both books.list dataset and isbn format exist and then surround the program with %macro and %mend statements.
See the following test:
SAS CODE:
options mlogic mprint;
proc format;
value isbn 1 = 'one'
other 'other';
run;
libname books "t:\temp";
data books.list;
length bookid 4 author $ 30 isbn 8 yearpub 8;
stop;
run;
%macro want;
data _null_;
set books.list;
file "t:\temp\list2.txt" dlm='09'x;
put bookid:4. author:$30. isbn:isbn18. yearpub:comma6.;
run;
%mend want;
%want
LOG:
42 options mlogic mprint;
43 proc format;
44 value isbn 1 = 'one'
45 other 'other';
NOTE: Format ISBN is already on the library.
NOTE: Format ISBN has been output.
46 run;
NOTE: PROCEDURE FORMAT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
47 libname books "t:\temp";
NOTE: Libref BOOKS was successfully assigned as follows:
Engine: V9
Physical Name: t:\temp
48 data books.list;
49 length bookid 4 author $ 30 isbn 8 yearpub 8;
50 stop;
51 run;
NOTE: Variable bookid is uninitialized.
NOTE: Variable author is uninitialized.
NOTE: Variable isbn is uninitialized.
NOTE: Variable yearpub is uninitialized.
NOTE: The data set BOOKS.LIST has 0 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
52 %macro want;
53 data _null_;
54 set books.list;
55 file "t:\temp\list2.txt" dlm='09'x;
56 put bookid:4. author:$30. isbn:isbn18. yearpub:comma6.;
57 run;
58 %mend want;
59 %want
MLOGIC(WANT): Beginning execution.
MPRINT(WANT): data _null_;
MPRINT(WANT): set books.list;
MPRINT(WANT): file "t:\temp\list2.txt" dlm='09'x;
MPRINT(WANT): put bookid:4. author:$30. isbn:isbn18. yearpub:comma6.;
MPRINT(WANT): run;
NOTE: The file "t:\temp\list2.txt" is:
Filename=t:\temp\list2.txt,
RECFM=V,LRECL=256,File Size (bytes)=0,
Last Modified=17Nov2014:14:33:22,
Create Time=17Nov2014:14:10:19
NOTE: 0 records were written to the file "t:\temp\list2.txt".
NOTE: There were 0 observations read from the data set BOOKS.LIST.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
MLOGIC(WANT): Ending execution.
Note: I had to correct the syntax error after comma6.
CTorres
This macro produces your code
%macro universal(code);
&code
%mend;
%universal(
%str(data _null_;
set books.list;
file "list2.txt" dlm='09'x;
put bookid:4. author:$30. isbn:isbn18. yearpub:comma6.;
run;)
);
You might want to clarify why you want it in a macro, e.g. parameters etc. otherwise you will get some interesting results.
(Nice one Vladislaff).
What about:
data _null_;
my_code="data _null_; set books.list; file '<filename>' dlm=','; put _all_; run;';
call execute(tranwrd(strip(my_code),"<filename>","list.txt");
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.