BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

%let i=1 ;

%let or1=europe ;

%let europe1=germany ;

%let europe_germany1=berlin ;

%let europe_germany_berlin1=capital ;

 

/*first*/

%put &&or&i ;   /*europe*/

 

/*second*/

%put &&&&&&or&i..&i ; /*germany */

 

/*third*/

%put  &&&&&&&&&&or&i.._&&&&&&or&i..&i..&i ; /*berlin*/

 

 

/*fourth*/

%put &&&&&&&&&&&&&&&&&&or&i.._&&&&&&or&i..&i.._&&&&&&&&&&or&i.._&&&&&&or&i..&i ; /*not coming*/

 

If I run above 4 steps 3 steps coming correctly and fourth one not resolving .

 

How many ampersands should I keep to  get value 'capacity' .

And one more question how to count to keep particular ampersands for fourth step .

14 REPLIES 14
mkeintz
PROC Star

I suggest the statement

  OPTIONS SYMBOLGEN

to get a report on how the macro processor is parsing your macro expressions.

 

You can then track step-by-step how it's being interpreted.  Remember that the macro parses the expression iteratively.  I.e. a each (disjoint) && becomes & from left to right.   Then the process starts again at the left end of the expression.

--------------------------
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

--------------------------
thanikondharish
Calcite | Level 5
thank you and who to get capital value please give code
AhmedAl_Attar
Rhodochrosite | Level 12

@thanikondharish 

 

To simplify the code for everyone (You & others looking at your code), I would do it this way

%let i=1 ;
%let or1=europe ;
%let europe1=germany ;
%let europe_germany1=berlin ;
%let europe_germany_berlin1=capital ;

%let continent =%superq(or&i);
%let country=%superq(%str(&&or&i)&i);
%let city=%superq(%upcase(&continent%str(_)&country&i));
%let city_type=%superq(%upcase(&continent%str(_)&country%str(_)&city&i));
%put &continent &country &city &city_type;

Hope this helps,

Ahmed

thanikondharish
Calcite | Level 5
thank you and can we do like see my coding fourth step almost 3 steps came
and fourth one how to get value 'capital'
AhmedAl_Attar
Rhodochrosite | Level 12
I see your original code, and honestly, If I inherited your code, I would re-write it, as it's not maintainable, and I would not attach my name to code that looks like that.

Sorry for being bluntly honest.
Ahmed
Tom
Super User Tom
Super User

https://www.amazon.com/Doctor-hurts-when-Jokes-Cartoons/dp/150043230X

 

It is not worth it to figure that out.  Build the name in pieces using multiple macro variables and multiple %LET statements. 

 

Or better still don't store data in macro variables.  Explain what problem you are trying to solve by storing data into macro variables with numeric suffixes on the end of the macro variable names.  I am sure there is a way to solve the problem that does not need to use such a complex storage system.

thanikondharish
Calcite | Level 5
I need value capital without using macro variable europe_germany_berlin1
Tom
Super User Tom
Super User

@thanikondharish wrote:
I need value capital without using macro variable europe_germany_berlin1

So where is the value CAPITAL stored? In your example is was in the macro variable EUROPE_GERMANY_BERLIN1. 

%let europe_germany_berlin1=capital ;

Do you have it stored somewhere else?

Oligolas
Barite | Level 11

Hi,

knowing the result, I used a brute force method to find exactly what you were looking for.

If you want undebugable crappy code that almost looks like a reverse encrypted md5 checksum try this 😂:

%put &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&or&i.._&&&&&&or&i..&i.._&&&&&&&&&&or&i.._&&&&&&or&i..&i..&i..&i.; /*capital*/

More seriously, take a look at @AhmedAl_Attar 's clean little piece of code

________________________

- Cheers -

thanikondharish
Calcite | Level 5
thank you it working I have tried with 22 and 24 ampersands in your code
almost
around 64 ampersands how did you keep this much of ampersands ,How did you
calculate .
symbelgen is just how resolving the macro variable
Oligolas
Barite | Level 11

Hi,

"brute force" means I tried adding ampersands until I get the result.

I redirected the output to an external log file (>400MB) and searched the syntax issuying 'capital' in the output 😎

Re-think your code if you ever need more than 2 consecutives ampersands

________________________

- Cheers -

ChrisNZ
Tourmaline | Level 20

> it working I have tried with 22 and 24 ampersands in your code almost around 64 ampersands how did you keep this much of ampersands 

If you wrote code like this you'd be fired. It's impossible to read.

 

images.png

 

 

ballardw
Super User

If you are attempting to do some sort of look up from one value to another perhaps a format is the way to accomplish this. If this data does not change often then you may find this easier that something attempting to use that many indirect macro references.

 

proc format;
Value $capital
"GERMANY"="Berlin"
"FRANCE" ="Paris";
run;


%let country=germany;
%let capital = %sysfunc(putc(%upcase(&country.),$capital.));
%put capital is &capital.;

One of the things that makes formats powerful is a single place to change things. If a new country gets added modify the code (or the data set that could generate the format) and execute the proc format code. If the format is placed in a permanent library and the format search path then the current format is always available.

proc format;
Value $capital
"GERMANY"="Berlin"
"FRANCE" ="Paris"
"NEWPLACE" = "Some City";
;
run;


%let country=newplace;
%let capital = %sysfunc(putc(%upcase(&country.),$capital.));
%put capital is &capital.;

for example to add to the format.

 

The %UPCASE in the PUTC is used because the value comparison done by Proc Format values is case sensitive by default and use of %upcase will result in one value to search for.

 

I strongly suspect the whole approach is poor at best, hence the origin of the question.

 

I've only been programming in SAS off and on for 33 years. I have used an indirect reference macro variable with &&& maybe 4 times. And after reconsidering it usually indicated I should use a different approach to creating the values needed.

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
  • 14 replies
  • 1134 views
  • 9 likes
  • 7 in conversation