DATA Step, Macro, Functions and more

Macro-change value of paraemter in title to another value

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 95
Accepted Solution

Macro-change value of paraemter in title to another value

Hello

I have a parameter that get values : a,b,c,d,e

I have proc print that run on tables:  tbl_a  ,   tbl_b   ,tbl_c,   tbl_d,   tbl_e

And in each report there is a title statement  (Title "&Param.")

My request :

 

Each time that value "a"  is on title of proc print I want to write "Group1".

Each time that value "b"  is on title of proc print I want to write "Group2".

Each time that value "c"  is on title of proc print I want to write "Group3".

Each time that value "d"  is on title of proc print I want to write "Group4".

Each time that value "e"  is on title of proc print I want to write "Group5".

How can I do it please

 

 


Accepted Solutions
Solution
‎04-09-2018 12:46 AM
Super User
Posts: 6,542

Re: Macro-change value of paraemter in title to another value

Given this structure to the program, I would replace the existing TITLE statement with:

 

Title

%if &param=a %then '0-20';

%else %if &param=b %then '20-40';

%else %if &param=c %then '40+';

;

 

For a limited number of values, %IF/THEN handles it well.

View solution in original post


All Replies
Respected Advisor
Posts: 2,658

Re: Macro-change value of paraemter in title to another value

It sure feels like you left something out of the explanation here.

 

Do you mean this:

 

A macro that knows a is the first letter of the alphabet and so it replaces a with group1

and it knows that b is the second letter of the alphabet and so it replaces b with group2

and it knows that m is the 13th letter of the alphabet and so it replaces m with group13

 

Or do you mean something else? (please explain in a lot more detail)

--
Paige Miller
Trusted Advisor
Posts: 1,826

Re: Macro-change value of paraemter in title to another value

Posted in reply to PaigeMiller

The most simple way is to assign 2 macro variables

    when mac1 = a then mac2 = Group2

etc.

Super User
Posts: 6,542

Re: Macro-change value of paraemter in title to another value

Luckily, this is easy.

 

Unluckily, the best way to do it depends highly on what you have coded so far.

 

Show what your macro code (and macro calls) look like without a TITLE statement.  If you have an otherwise-working macro, inserting the TITLE statement will be easy.  But if your macro code doesn't work, even without a TITLE statement, you have to fix that first.

Frequent Contributor
Posts: 95

Re: Macro-change value of paraemter in title to another value

Posted in reply to Astounding

I am at home so dont have SAS but i will write an example to simple code and what I need.

In the code you can see a statement  Title “&param1.”;

I want that instead of written ’a’  to be written '0-20'

I want that instead of written ’b’  to be written '20-40'

I want that instead of written b’  to be written '40+'

 

data tbl;

input  x  y;

cards;

1  10

2  15

3  30

4  40

5  50

;

Run;

 

Data tbl2;

Set tbl1;

IF y<=20 then Ind=’a’;

Else If y>20 and y<=40 then Ind=’b’;

Else If y>40 then Ind=’c’;

Run;

 

 

%let vector=a+b+c;

%macro test;

%do i=1 %to 3;

%let param1 = %scan(&vector,i,+);

PROC SQL;

Create table  Summarry_&param1.  AS

SELECT  count(*)  as NoObs

FROM tbl2

 WHERE  Ind=&param1.

QUIT;

Title “&param1.”;

Proc print data= Summarry_&param1.    Noobs;

Run;

%end;

%mend test;

Super User
Posts: 22,857

Re: Macro-change value of paraemter in title to another value

You can set up an account under SAS Academics on Demand (Independent Learner) to get access to a cloud version of SAS.
Solution
‎04-09-2018 12:46 AM
Super User
Posts: 6,542

Re: Macro-change value of paraemter in title to another value

Given this structure to the program, I would replace the existing TITLE statement with:

 

Title

%if &param=a %then '0-20';

%else %if &param=b %then '20-40';

%else %if &param=c %then '40+';

;

 

For a limited number of values, %IF/THEN handles it well.

Respected Advisor
Posts: 2,658

Re: Macro-change value of paraemter in title to another value

[ Edited ]
Posted in reply to Astounding

I agree with @Astounding.

 

This is very easy to do, as he has shown.

 

What I don't understand about the original question, which you @Ronein really haven't explained, is do you have a maximum of three possibilites, a b and c, or could the list be much much longer in which case the solution by @Astounding becomes cumbersome. So, @Ronein, please clear this up for us. The better information you give us, the better solutions you will get.

 

And please answer my original question, do you want the macro to realize that b is the second letter of the alphabet, and hence you get the second item in sequence in the title?????? Or not? These are very different situations. We can't advise you unless we know.

 

By the way, depending on your answer, I could envision a situation where macros ARE NOT NEEDED, and a much simpler solution is available, using BY groups in the analysis and the #Byval construct in your title. Again, let me repeat ... you MAY NOT NEED macros. There may be a MUCH SIMPLER solution. Please please please explain.

--
Paige Miller
Frequent Contributor
Posts: 95

Re: Macro-change value of paraemter in title to another value

Posted in reply to Astounding

Hello

Your code is not working I think.

Also after title statement should have quotes " "  and you dont have it in your code

 

 

Ron

Super User
Posts: 13,066

Re: Macro-change value of paraemter in title to another value


@Ronein wrote:

Hello

Your code is not working I think.

Also after title statement should have quotes " "  and you dont have it in your code

 

 

Ron


 

"is not working " is awful vague. And which code???

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super User
Posts: 22,857

Re: Macro-change value of paraemter in title to another value

Some approaches:

1. A whole bunch of %IF/%THEN statements

2. A user defined format and apply it using %SYSFUNC

3. Manually set another set of macro variables that line with the same index. 

Frequent Contributor
Posts: 95

Re: Macro-change value of paraemter in title to another value

Hello
I just gave a simple example.
In reality it was more complicated.
The task is to change parameter value in title statement.
I have received an amswer to use if then statement within title statement.
Thank you so much
☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 193 views
  • 0 likes
  • 6 in conversation