BookmarkSubscribeRSS Feed
DPM1213
Calcite | Level 5

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.

3 REPLIES 3
CTorres
Quartz | Level 8

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

Vladislaff
SAS Employee

This macro produces your code Smiley Happy

%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;)

);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

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
  • 3 replies
  • 824 views
  • 0 likes
  • 4 in conversation