BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JeffreyLowe
Obsidian | Level 7

Good Day!

 

I am trying to call an email macro from an IF based on a stored value. I am sending to different individuals based on the setting of flag.

 

data _null_ ;

     if flag = 'T" then ;

          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;

     else ;

          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;

run ;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Since the data step variable FLAG never equals "T", you will always execute the ELSE clause, but you have syntax errors and incorrect semi-colons.

 

data _null_ ;
     if flag = "T" then 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
     else 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
run ;

but I think you might really want a SET statement so FLAG has a value that might be "T"

 

 

data _null_ ;
    set somedataset;
     if flag = "T" then 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
     else 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
run ;

And then whatever %genmail does can only contain DATA step code, if there's a PROC in there, the whole thing fails.

 

But, there are lots of problems here. Whether FLAG="T" or not, you execute the same macro with the same arguments. Why? 

 

Perhaps you ought to start over and explain what you are trying to do, rather than putting up such problematic code and have us guess what you are trying to do.

--
Paige Miller

View solution in original post

8 REPLIES 8
Reeza
Super User
Your IF syntax is wrong you shouldn't have the semicolon between them. In that case, it ends your IF statement and each macro command will be executed.

data _null_ ;

if flag = 'T" then

%genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;

else

%genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;

run ;

In general though, I'd recommend creating a variable with the parameters and then using that with the macro call. Also, within a data step it's slightly cleaner to use CALL EXEUCTE() to call the macro. The documentation has a good example of CALL EXECUTE.

data _null_ ;

if flag = 'T" then ;

call execute('%genMail("tomail","ccmail","bcmail","subject","attachment","bodytext");') ;

else ;

call execute('%genMail("tomail","ccmail","bcmail","subject","attachment","bodytext");') ;

run ;
PaigeMiller
Diamond | Level 26

Since the data step variable FLAG never equals "T", you will always execute the ELSE clause, but you have syntax errors and incorrect semi-colons.

 

data _null_ ;
     if flag = "T" then 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
     else 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
run ;

but I think you might really want a SET statement so FLAG has a value that might be "T"

 

 

data _null_ ;
    set somedataset;
     if flag = "T" then 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
     else 
          %genMail("tomail","ccmail","bcmail","subject","attachment","bodytext") ;
run ;

And then whatever %genmail does can only contain DATA step code, if there's a PROC in there, the whole thing fails.

 

But, there are lots of problems here. Whether FLAG="T" or not, you execute the same macro with the same arguments. Why? 

 

Perhaps you ought to start over and explain what you are trying to do, rather than putting up such problematic code and have us guess what you are trying to do.

--
Paige Miller
JeffreyLowe
Obsidian | Level 7

Actually flag is set elsewhere based on the data being processed when I entered my example it should say if &flag = 'T'..........

PaigeMiller
Diamond | Level 26

@JeffreyLowe wrote:

Actually flag is set elsewhere based on the data being processed when I entered my example it should say if &flag = 'T'..........


This syntax works only if the value of &FLAG is a character string with quotes around it ... in which case, why even bother with DATA _NULL_;? Do it all with macro %IF and not within a DATA step.

 

%if &flag=T %then ... ; /* Note, you don't put macro variable values in quotes */
--
Paige Miller
Reeza
Super User
As of SAS 9.4 M5+ (I think) you can use %IF in open code and no macro/data step is required.

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=mcrolref&docsetTarget=n18fij8...
PaigeMiller
Diamond | Level 26

@PaigeMiller wrote:

@JeffreyLowe wrote:

Actually flag is set elsewhere based on the data being processed when I entered my example it should say if &flag = 'T'..........


This syntax works only if the value of &FLAG is a character string with quotes around it ... in which case, why even bother with DATA _NULL_;? Do it all with macro %IF and not within a DATA step.

 

%if &flag=T %then ... ; /* Note, you don't put macro variable values in quotes */

Adding to the above ... if you really want to test &flag='T', this is a situation where double quotes and single quotes cannot be mixed and matched. If, for example, the value of &flag is "T", you will not get a match. Only if &flag is 'T' will you get a match, and this doesn't seem like a good way to do the comparison (which is one reason why I say macro variable values should not be enclosed in quotes).

--
Paige Miller
JeffreyLowe
Obsidian | Level 7

Thank you Paige!

PaigeMiller
Diamond | Level 26

Adding:

 

Yes, I agree with @Reeza , that CALL EXECUTE would be a better method of executing this macro if it doesn't contain valid DATA step code.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 884 views
  • 5 likes
  • 3 in conversation