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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 1335 views
  • 0 likes
  • 4 in conversation