BookmarkSubscribeRSS Feed
thejianguy
Calcite | Level 5

Hi all,

 

I'm not sure if there is any solution that i might have missed out, but just looking for some help.

 

I am trying to make my column variables dynamic. The code is as follows:

%Let len = 7;
%Let col = TEST;
%Let colano = %col%str(_ANONMY);

Data help;
    Set Work.dataset;
    &colano = cats(of new1 - new&len)
Run;
 

 

This line:

&colano = cats(of new1 - new&len)

returns me an error message of ERROR 180-322: Statement is not valid or it is used out of proper order.

 

Is there anyway i can work around it? There are other parts of the code that i would require to use &col elsewhere, which explains why i need 2 lines of macro to separately define them. (I opened OPTIONS SYMBOLGEN and the macro &len seems to be working fine.)

 

Thanks for your time to those who are reading this!

3 REPLIES 3
Astounding
PROC Star

This line raises a couple of red flags:

 

%Let colano = %col%str(_ANONMY);

 

Try it this way:

 

%Let colano = &col._ANONMY;
Reeza
Super User
Have you considered arrays? Is this your actual use case?
Kurt_Bremser
Super User

Maxim 2: Read The Log.

37         %Let len = 7;
38         %Let col = TEST;
39         %Let colano = %col%str(_ANONMY);
WARNING: Apparent invocation of macro COL not resolved.

So your syntax ERROR 180 is caused by the earlier problem in the %let. Always do your debugging top-down, eliminating issues in the order they appear in the log. Often fixing the first problem will do away with most or all of the others. Do NOT let WARNINGs persist. They are there for a reason and have to be dealt with (Maxim 25).

The result of your faulty %let is this:

40         %put "&colano.";
WARNING: Apparent invocation of macro COL not resolved.
"%col_ANONMY"

Since the (unresolvable) percent sign can not be used in a valid SAS name, the data step statement will fail.

To concatenate a macro variable value with a string, use this:

%Let colano = &col._ANONMY;
%put "&colano.";

Log:

38         %put "&colano.";
"TEST_ANONMY"

Now the macro variable colano contains a valid SAS name.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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