Create if statements based on value of macro parameter

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

Create if statements based on value of macro parameter

[ Edited ]

I have a macro program that I've written that uses multiple parameters. 

 

One of these parameters is the clients' name. The macro looks something like this:

 

 


%macro  Program(
CampaignCode,
ClientCode,
Start,
End,
Response
)

 

 

The commands that are called using the macro are pages and pages long but there is a certain place where I want to create a set of conditional statements for calculations based on the value of &ClientCode

 

Basically something like this

 

data work.want;

set work.have;

if &ClientCode = "Client1" then Calculation = VAR1 +VAR2+VAR3;

else if &ClientCode= "Client2" then Calculation = VAR1 + VAR3 + VAR4;

else if &ClientCode = "Client3" then Calculation = Var2 + VAR4 + VAR6;

.....;

run;

 

But that is not working correctly. 

 

I feel like this is a relatively simple command I am trying to execute. Can someone show me the correct synthax? 

 

Thanks

 

 


Accepted Solutions
Solution
‎02-22-2018 03:26 PM
Super User
Posts: 6,761

Re: Create if statements based on value of macro parameter

Posted in reply to SmcGarrett

For that, you just need to add quotes:

 

if "&ClientCode" = .....

 

They have to be double-quotes, not single-quotes.

View solution in original post


All Replies
Super User
Posts: 6,761

Re: Create if statements based on value of macro parameter

Posted in reply to SmcGarrett

How do these calculations depend on the value of &Response ?

 

The correct syntax depends on what is contained in the macro variables.  In particular, what is the value for &ClientCode ?

Contributor
Posts: 41

Re: Create if statements based on value of macro parameter

Posted in reply to Astounding

The Value would be a string of 4 characters that are a code for the client. 

 

Some clients calculate their total budget based on different combinations of Variables

 

 

Contributor
Posts: 41

Re: Create if statements based on value of macro parameter

Posted in reply to Astounding

Ah...I see...that was a typo. It would not be dependent on Response...ONLY ClientCode. 

 

Sorry.

Solution
‎02-22-2018 03:26 PM
Super User
Posts: 6,761

Re: Create if statements based on value of macro parameter

Posted in reply to SmcGarrett

For that, you just need to add quotes:

 

if "&ClientCode" = .....

 

They have to be double-quotes, not single-quotes.

Contributor
Posts: 41

Re: Create if statements based on value of macro parameter

Posted in reply to Astounding

OMG I knew it was something simple. Was way overthinking it! Thank you! 

 

Super User
Super User
Posts: 8,083

Re: Create if statements based on value of macro parameter

Posted in reply to SmcGarrett

Not sure what RESPONSE has to do with your question as the code you posted is referencing the parameter CLIENTCODE instead.

So code like this:

data work.want;
  set work.have;
  if &ClientCode = "Client1" then Calculation = VAR1 +VAR2+VAR3;
  else if &ClientCode= "Client2" then Calculation = VAR1 + VAR3 + VAR4;
  else if &ClientCode = "Client3" then Calculation = Var2 + VAR4 + VAR6;

Will work you you set CLIENTCODE equal to something that evaluates to a character string.  So you could call it with a string literal.

%program(clientcode="Client2",...)

Or you could call it with the name of a character variable that is in the HAVE dataset.

%program(clientcode=Client_Id,...)

Or even a function call that can run in the generated IF statement as long it the result is a character string that the IF statement can compare to the literal character values.

 

 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 117 views
  • 0 likes
  • 3 in conversation