2

I am trying to use a macro to label some array variables in SAS to avoid having to type a lot of lines of code. What I'm trying to do is basically this:

%macro LABEL_ARRAY(V);
 %DO I = 1 %TO 4;
   %let variablename=&V(&I);
   array1[&I] = "Value of &variablename"
 %END
%MEND LABEL_ARRAY;

So, V is an array containing the corresponding variable names for the positions in the array array1. I'm trying to do this for more than 4 variables per array and for numerous arrays, but that's the basic idea. Basically what is happening is the array1[&I] which I would want to eventually say array1[1] for the first entry, is not using the value of I but rather just saying &I, same thing with &variablename.

Any suggestions on what might be happening? Thanks.

kyro1021
  • 31
  • 2
  • you might try %let variablename=&&V(&I); to see if that will work but I suspect not. Actually change it to: %let variablename=&&V&I; and that should work – DCR Apr 07 '16 at 20:45
  • I'm confused as to what you want. Can you show a data step that uses your macro? You say you want to label variables but there is no LABEL statement. Array1[&i] will resolve to array1[1] as you say you want. &Variablename will not resolve to a variable name. You may want to look at VNAME function. But you can't use a data step array to assign variable labels. – Quentin Apr 08 '16 at 00:27
  • @Quentin Presumably this builds a bunch of label statements that is then used like `label %label_array(v);` – Joe Apr 08 '16 at 02:52
  • Thanks @joe. The array reference through me. – Quentin Apr 08 '16 at 03:09
  • Arrays in SAS are only shortcut references to variables. They hold no meaning beyond that. If you're looking to apply labels to sets of variables there are MUCH better ways - but you need to clarify your rules. – Reeza Apr 08 '16 at 03:45
  • @Quentin, Joe is right, I want to do exactly that: build a bunch of label statements. – kyro1021 Apr 08 '16 at 14:57
  • @Reeza, what do you mean by clarify my rules? You are correct in saying I want to apply labels to a set of variables, specifically to each element of an array. – kyro1021 Apr 08 '16 at 14:59
  • DATA step array references can't be used in a label statement. Joe's solution describes macro arrays as one potential approach. If you edit your question to include small amount of sample data and desired labels you want to generate, will be easier for people to provide solutions. – Quentin Apr 08 '16 at 15:09
  • @kryo1021 where is the label coming from? How do you know which label matches which variable? – Reeza Apr 08 '16 at 15:30

1 Answers1

2

There is no built-in array type in the macro language (there is in fact no types at all in the macro language) in SAS.

Macro arrays are instead handled through multiple resolution of the ampersand. I encourage you to read this answer for more information on exactly how this works in the general case.

However, for the specific case of macro variable "arrays", it is worth explaining exactly how they work.

What you have is likely a set of macro variables like so:

%let v = Color;
%let color1=Blue;
%let color2=Red;
%let color3=Green;

And you want to be able to do:

%do _i = 1 to 3;
  %let thisColor = &color&_i;
%end;

Except that doesn't work - because &color resolves (and likely resolves to &color if you didn't separately define taht), and &i resolves to 1, leaving you with &color1 but not Blue, plus a warning message about &color not resolving.

What you need to do is delay the resolution of &color until &_i resolves. You can do that by adding a second ampersand.

%do _i = 1 to 3;
  %let thisColor = &&color&_i;
%end;

What that does is tells SAS to resolve the two && to one & but keep it in the queue for resolving; then on a second pass, resolve that single &. So you have

&&color&_i
&color1
Blue

Instead of stopping at the second level.

This is explained in more detail in my paper Unravelling the Knot of Ampersands, and Ron Fehd's Array: Construction and Usage of Arrays of Macro Variables.

Community
  • 1
  • 1
Joe
  • 62,789
  • 6
  • 49
  • 67
  • In addition to this I would encourage you to consider alternatives to the macro variable array, as it is often easier to do things like this with data-driven programming (google SAS+that). – Joe Apr 07 '16 at 21:26