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

The pseudo code below will demonstrate what I would like to do. 


Assume I have a macro of the sort: 

 

%MyMacro(Part1YesOrNO, Part2YesOrNO); 
   Some code representing "Part 1"; 
      .
      . 
      . 
   Some code representing "Part 2"; 
%mend MyMacro; 

Now, if the user types in: 

 

 

%MyMacro(Yes, Yes); 

both parts of the Macro should be run. 

 

 

If the user types in:

%MyMacro(Yes, No); 

Only part 1 of the Macro should be run. 

Any advice of how I could achieve this kind of functionality? 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%MyMacro(Part1YesOrNO, Part2YesOrNO); 
    %if %upcase(&part1yesorno)=YES %then %do;
   Some code representing "Part 1"; 
    %end;
      . 
   %if %upcase(&part2yesorno)=YES %then %do;   . 
   Some code representing "Part 2"; 
    %end;
%mend MyMacro; 

Hint: don't use such long variable names unless they are absolutely necessary, I would create the macro as follows

 

%macro mymacro(part1,part2);

unless you are a much better typist than I am, I find shorter name that still have meaning a much better approach, its less typing and less chance for error. Also, I prefer values of Y or N to Yes or No, again simplicity and less typing and less chance for error.

--
Paige Miller

View solution in original post

3 REPLIES 3
tbellmer_wf
Fluorite | Level 6

%macro mymacro(part1yesorno, part2yesorno);

  %if %upcase(%substr(&part1yesorno, 1, 1)) = Y %then %do;

      /* your part1 code goes here */

  %end;

 

  %if %upcase(%substr(&part2yesorno, 1, 1) = Y %then %do;

      /* your part2 code goes here */

  %end;

%mend;

 

%mymacro(Yes, No);

PaigeMiller
Diamond | Level 26
%MyMacro(Part1YesOrNO, Part2YesOrNO); 
    %if %upcase(&part1yesorno)=YES %then %do;
   Some code representing "Part 1"; 
    %end;
      . 
   %if %upcase(&part2yesorno)=YES %then %do;   . 
   Some code representing "Part 2"; 
    %end;
%mend MyMacro; 

Hint: don't use such long variable names unless they are absolutely necessary, I would create the macro as follows

 

%macro mymacro(part1,part2);

unless you are a much better typist than I am, I find shorter name that still have meaning a much better approach, its less typing and less chance for error. Also, I prefer values of Y or N to Yes or No, again simplicity and less typing and less chance for error.

--
Paige Miller
mkeintz
PROC Star

Consider:

 

%MyMacro(Part1YesOrNO, Part2YesOrNO); 
  %if &part1yesorno=Yes %then %do;
     *Some code representing "Part 1"; 
  %end;
  %if &part2yesorno=Yes %then %do;
     *Some code representing "Part 2"; 
  %end;
%mend MyMacro; 
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 526 views
  • 3 likes
  • 4 in conversation