BookmarkSubscribeRSS Feed
KeinYung
Calcite | Level 5

Write a macro program to find out how many ampersands is needed for generating the specified result.

Given a series of macro variables:

 

%let a=b;

%let b=c;

%let c=d;

%let d=e;

 

 

 

Pls help. Thank you.

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

What have you tried so far?

gamotte
Rhodochrosite | Level 12

Hello,

 

What do you need that for ?

If you need more that three ampersands to resolve a macrovariable there

is probably a design problem in your code.

 

%let TRUE=GE;
%let GE=AND;
%let AND=LE;
%let LE=TRUE;

%macro MinAmpersand(var);

    data _NULL_;
	    length value $4. amps $20.;
	    var="&var.";

		amps="&";

		do while (var ne value and MAX_LOOP<20);
		    MAX_LOOP+1;
		    value=resolve(cats(amps,"TRUE"));
			amps=cats(amps,"&");
		end;

		nbamps=length(amps)-1;

		put nbamps " ampersands necessary for macrovariable &var.";
	run;

%mend;

%MinAmpersand(TRUE);
%MinAmpersand(GE);
%MinAmpersand(AND);
%MinAmpersand(LE);
SASKiwi
PROC Star

I see a similar question has been asked here: https://communities.sas.com/t5/SAS-Procedures/Count-the-number-of-ampersands/m-p/518736#M73337

 

I can only assume it is some sort of homework assignment. Personally I don't see the value in these types of intellectual challenges.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 858 views
  • 0 likes
  • 4 in conversation