Help using Base SAS procedures

Questions regarding macros used for proc format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Questions regarding macros used for proc format

Hi,

 

I am trying to create a macro for proc format as I have many variables that can be formatted in similar matter. However, I have 2 problems/questions.

 

1. Is there a way I could use a comma in my macro without SAS thinking it is an additional parameters?

%macro man_e(i,j,k);
	value &&i.
		1 = "&&j."
		2 = "Did not &&k.";
%mend;


%man_e(MAN,Appointed surviving spouse of X, of Y or of Z,appoint surviving spouse of X, of Y or of Z);

 

2. Can I use an apostrophe in the macro? It would be very useful as a lot a apostrophe are used in French

%man20_e(MAN_20Ae,au niveau d'entrée);

 

Thank you in advance!


Accepted Solutions
Solution
‎03-26-2018 01:50 PM
Super User
Posts: 6,785

Re: Questions regarding macros used for proc format

You should be able to solve both at once, by adding double-quotes when  you call the macro.  For example:

 

%macro man_e (i, j, k);

 

   value &i.

    1 = &j.

    2 = &k.;

 

%mend;

 

proc format;

%man_e (MAN, "Appointed surviving spouse of X, of Y or of Z", "Did not appoint surviving spouse of X, of Y or of Z")

 

That makes it more readable, and allows you to use commas and apostrophes, with one small downside.  You have to type the words "Did not" as part of the value of your third parameter.

View solution in original post


All Replies
Respected Advisor
Posts: 3,067

Re: Questions regarding macros used for proc format

[ Edited ]

1. Sure you can do this, but I don't see how this is any less typing or any easier than typing PROC FORMAT for your cases without the macros. If you must have a comma, use the %str() function.

 

2. Look up the %STR and %NRSTR function in SAS, there are examples there of including apostrophe's in your macro

--
Paige Miller
Super User
Posts: 13,583

Re: Questions regarding macros used for proc format

You might look into creating a data set for use with the cntlin option to create formats.

Your I parameter would be the FMTNAME, your J and K parameters would be used to create the LABEL and the values 1 and 2 would be Start.

 

 

Solution
‎03-26-2018 01:50 PM
Super User
Posts: 6,785

Re: Questions regarding macros used for proc format

You should be able to solve both at once, by adding double-quotes when  you call the macro.  For example:

 

%macro man_e (i, j, k);

 

   value &i.

    1 = &j.

    2 = &k.;

 

%mend;

 

proc format;

%man_e (MAN, "Appointed surviving spouse of X, of Y or of Z", "Did not appoint surviving spouse of X, of Y or of Z")

 

That makes it more readable, and allows you to use commas and apostrophes, with one small downside.  You have to type the words "Did not" as part of the value of your third parameter.

Super User
Super User
Posts: 8,127

Re: Questions regarding macros used for proc format

[ Edited ]

@Shawn08 wrote:

Hi,

 

I am trying to create a macro for proc format as I have many variables that can be formatted in similar matter. However, I have 2 problems/questions.

 

1. Is there a way I could use a comma in my macro without SAS thinking it is an additional parameters?

%macro man_e(i,j,k);
	value &&i.
		1 = "&&j."
		2 = "Did not &&k.";
%mend;


%man_e(MAN,Appointed surviving spouse of X, of Y or of Z,appoint surviving spouse of X, of Y or of Z);

 

2. Can I use an apostrophe in the macro? It would be very useful as a lot a apostrophe are used in French

%man20_e(MAN_20Ae,au niveau d'entrée);

 

Thank you in advance!


I am not sure what you mean for (1) but if the issue is how to pass in a list of values then just use something other than comma as your delimiter.  For example you could use | as the delimiter.

%macro man_e(fmtname,values);
%local i ;
value &fmtname
%do i=1 %to %sysfunc(countw(&values,|));
  &i = %sysfunc(quote(&qscan(&values,&i,|)))
%end;
;
%mend;

 

For (2) add quoting.  Either write the macro to expect a quoted string. 

%man20_e(MAN_20Ae,"au niveau d'entrée");

Or add macro quoting.

%man20_e(MAN_20Ae,%bquote(au niveau d'entrée));
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 149 views
  • 0 likes
  • 5 in conversation