New Titles within a Macro

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

New Titles within a Macro

I am trying to add unique titles to my outputs for proc means and proc univariate results, but the only way I know how to do this is through the insertion of a macro variable which can only be one word. I could use "_" but it needs to be cleaner. Anyone know of a way to create unique titles within a macro process?

 

What I have so far looks like this:

 

data have;

set hadbefore;

run;

 

%let varA=

abc

def

ghi

;

%let varB=

j    k     l

m  n    o

p   q    r

;

%macro test;

%do i=1 %to 3;

ods noproctitle;

%let v = %scan(&varA, &i.);

%let t = %scan(&varB, &i.);

title "&v.";

title2 "&t.";

proc means data=test;     (another similar program with proc univariate as well)

var &v;

run;

%end;

%mend test;

%test;

 

What it does for the  title 2 is prints one word for each instead of the whole line. I have tried adding in quotations but then it runs with an error because it picks up the first word with one quotation mark.

 

Any help would be great thanks.


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 19,194

Re: New Titles within a Macro

You use the scan function with no delimiter specified, which means the default delimiter is a SPACE. Add a delimiter to the macro variable and the corresponding delimiter to the SCAN function to separate the macro variables.

%let varB=
j k l|
m n o|
p q r
;
%let i=1;
%let t = %scan(&varB, &i., |);

%put &t;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,800

Re: New Titles within a Macro

but the only way I know how to do this is through the insertion of a macro variable which can only be one word
Macro variables can be as many words as you want them to be. So I don't understand this limitation you are stating.
What it does for the title 2 is prints one word for each instead of the whole line.
This also makes no sense to me. Can you give us an example of what title you would actually like to see?
Contributor
Posts: 39

Re: New Titles within a Macro

Okay, what I meant was what is stored only seems to be taking on one word values. For example, I want two titles in my output. The first title as the variable's name, and the second title as a description of that variable. So for the macro variable varA I store the variable name, and for the macro variable varB I want to store the variable description but I don't know the syntax for creating a macro variable that calls multiple words.

Super User
Super User
Posts: 7,727

Re: New Titles within a Macro

[ Edited ]

I do not understand why you have all that macro code?  Post some have and some want so its easier to see.  Anyways, SAS have specific caommands for dealing with this type of thing, its called by var processing.  First get your data into a normalised structure, its far easier to work with, and you can use by group processing - note its also CDISC standard.

dataset have looks like:

parameter  result     idvar

A                5            01

A                6            02

B                3            01

 

now I can just do:

proc means data=have;

  var result;

  by parameter;

  out=want n=n...;

run;

 

proc report data=want nowd;

   columns ...;

   title1 "This is the title for parameter = #byvar(parameter)";

   by parameter;

run;

 

So much simpler, no macro code.  Examples:

http://www2.sas.com/proceedings/sugi23/Coders/p75.pdf

 

Solution
‎09-25-2015 06:23 AM
Super User
Posts: 19,194

Re: New Titles within a Macro

You use the scan function with no delimiter specified, which means the default delimiter is a SPACE. Add a delimiter to the macro variable and the corresponding delimiter to the SCAN function to separate the macro variables.

%let varB=
j k l|
m n o|
p q r
;
%let i=1;
%let t = %scan(&varB, &i., |);

%put &t;

Contributor
Posts: 39

Re: New Titles within a Macro

Awesome thanks for the help this worked. Totally forgot about adding delimiters should've known from the error messages I was getting.

Valued Guide
Posts: 858

Re: New Titles within a Macro

Use underscore &i, so it will change the name for every iteration.

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 336 views
  • 0 likes
  • 5 in conversation