DATA Step, Macro, Functions and more

ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';

Posts: 63

ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';

ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';

Does the above code work. I am creating a macro variable to automate the output template.

Super Contributor
Posts: 543

Re: ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';


This is a very ambiguous question.

I guess it depends on what values you give to the macro variable &styl.

If it takes the values tagsets.excelxp, then yes, it works

but if it takes the value default (which may be one of the style you may want to use...)

So, what do you want to do?

The code below is a macro that creates an XML file using different styles (default, normal, statistical...)

%let path = your_path

%macro print(styl = );

    ods tagsets.excelxp file = "&path.\discharge_print_&styl..xml" style= &styl. ;

    proc print data = sashelp.class;where sex = "F";


%mend print;

%print(styl = normal);

%print(styl = default);

Not sure what you want.

Posts: 63

Re: ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';

I am sorry for confusing.

ods &styl file='C:\Users\sas\Desktop\discharge_print.&styl';

and when calling macro variable %sty(styl=pdf);

That means i am opting a pdf output.

Does this logic work?

Super Contributor
Posts: 1,636

Re: ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';


You have to use double " " when macro variables are involved.

ods &styl file='C:\Users\sas\Desktop\discharge_print.&styl ';

Super Contributor
Posts: 543

Re: ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';

So, yes it would work;

%macro print(styl = );

    ods &styl. file = "&path.\discharge_print.&styl."  ;

    proc print data = sashelp.class;where sex = "F";


   ods &styl. close;

%mend print;

%print(styl = pdf);

%print(styl = html);

Please note the "ods &styl. close" statement as well.

And yes, you should always use double quotes instead of single quotes in a macro.

Good luck,


Posts: 8,742

Re: ods &styl file='C:\Users\sas\Desktop\discharge_print.xml';


  In my opinion, &STYL is a confusing name to use for the macro variable. Here's why. When you have an ODS "sandwich"

ods <destination> file="somefile.ext" style=<style>;

...more code ...

ods <destination> close;

the <destination> should be specified as:

ods html

ods pdf

ods rtf

ods tagsets.excelxp

ods msofice2k

etc, etc.

None of those destinations is a STYLE definition name. The <destination> used by ODS is different from the STYLE definition used by ODS. for example, there is no style called PDF and there is no style called HTML or TAGSETS.EXCELXP... those are all destinations.

  A less confusing name for the macro variable would be &DEST, which would then allow you to use &STYL to change the style definition used for your output. Another possible macro variable might be &EXT, so that if you use a destination like MSOFFICE2K, you can specify the correct .HTML extension. Or you can specify a different style definition for each destination, or based on the destination, you can use a %IF to create a macro variable for other options (&OTHEROPT).

  I used some color coding in the program below to give you the general idea of the possible use for 3 different macro variables. I didn't do color coding for every macro variable reference, but there should be enough to convey the general concept of the difference between ODS destination, ODS style and the file extension for the file you are creating.

  This is just my .02 on the design of the macro program and naming the macro variables so they accurately reflect which code piece you are generating: the destination name, the file extension (which does NOT always match the destination name) and the style specification (which again, does not always match the destination name).


%macro alt(dest=, ext=, styl= );
  ods listing close;
  option nodate number pageno=1;
  %if %upcase(&dest)=TAGSETS.EXCELXP %then %do;
      %let otheropt = options(embedded_titles='yes');
  %if %upcase(&dest)=PDF %then %do;
      %let otheropt = notoc;
  %else %let otheropt=;
  ods &dest file = "c:\temp\use_&dest._&styl..&ext
            style=&styl &otheropt ;
    title "Using &dest with &styl style to create &ext type of file";
    proc print data = sashelp.class;
       where sex = "F";
  ods &dest close;
%mend alt;
%alt(dest=html, styl=htmlblue, ext=html)
%alt(dest=pdf, styl=journal, ext=pdf)
%alt(dest=rtf, styl=analysis, ext=rtf)
%alt(dest=msoffice2k, styl=normal, ext=html)
%alt(dest=tagsets.excelxp, styl=sasweb, ext=xml)

Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation