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

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 3 replies
  • 334 views
  • 3 likes
  • 4 in conversation