gdogg371
gdogg371

Reputation: 4132

Create a series of global macro variables named in ascending numeric value

I have built the following macro where the code iterates through numbers 1 to 82 and assigns a format defined in a proc format statement when creating that macro variable:

%macro metric_var;

/*macro to define 86 metric variables using proc format statement. this only needs doing once. to save processing time*/
/*this macro is outside of the main macro loop and the proc format statement is outside the do loop within the macro*/
/*as again it only needs defining once.*/

proc format;
value metricvar 

1 = a 2 = y 3 = b 4 = d 5 = b2
6 = r1 7 = b3 8 = q 9 = p 10 = o 

11 = c1 12 = d1 13 = c 14 = d2
15 = c6  16 = t1 17 = k2 18 = p2

19 = c8 20 = m5 21 = c3 22 = m6
23 = g 24 = m7 25 = h 26 = m8
27 = k1  28 = m3 29 = l 30 = m4
31 = l1  32 = t8

33 = w1 34 = g1  35 = w4 36 = g3
37 = p1 38 = g4 39 = m 40 = g5
41 = m2 42 = g6 

43 = k 44 = v 45 = j 46 = j1
47 = j5 48 = j3 49 = k4 50 = f6
51 = j6 52 = n 53 = k5 54 = n4 
55 = w 56 = s 57 = s1 58 = x
59 = j2

60 = v 61 = a5 62 = b6 63 = k3
64 = t7 65 = a2 66 = t6 67 = i5 

68 = l2 69 = e 70 = e7  71 = e8 
72 = e9 73 = b4 74 = j4 75 = p5  
76 = p6 77 = r 78 = r2 79 = r5   
80 = r6  81 = r7 82 = s7

/*don't forget now runs to 86 metrics*/
;
run;

%let metvar1 = 1;
%let metvar2 = 82;

%do U = &metvar1. %to &metvar2. %by 1;


data _null_;
a = &u.;
call symput('myvar',(put(a,metricvar.)));
run;

%let myvar_&u. = &myvar.;
%put &myvar.;
%put &myvar_&u.;


%end;
%mend;

%metric_var;

In this the macro variable &myvar. works correctly and is assigned the values in ascending order as I would like.

However this one variable is assigned a new value 82 times and at the end of the process it has the value of the last iteration.

What I have tried to do with the line %let myvar_&u. = &myvar.; is to then create 82 separate global variables that would be called &myvar_1. through to &myvar_82. that would each have a different value.

When the log runs the following warning appears

WARNING: Apparent symbolic reference MYVAR_ not resolved.
&myvar_79

It appears that the final variable, the one I want is not being assigned correctly and I'm not sure why.

Does anyone have any suggestions as to what the problem is?

Thanks

Upvotes: 1

Views: 562

Answers (2)

Joe
Joe

Reputation: 63424

You don't need a macro to do this. Honestly you probably don't need to do this at all - it's very likely that what you're using this for would be better done in data step(s) entirely - but even given your stated desire this is done in a data step.

data _null_;
do iter = 1 to 82;
call symputx(cats('myvar_',iter),put(iter,metricvar.));
end;
run;

After all, CALL SYMPUT(x) takes a character argument, not a constant - so you can just modify that 82 times.

Upvotes: 2

vasja
vasja

Reputation: 4792

%macro metric_var;
%let metvar1 = 1;
%let metvar2 = 82;

%do U = &metvar1. %to &metvar2. %by 1;
    %let myvar_&u. = &u;
    %put myvar_&u is: &&myvar_&u;
%end;
%mend;

%metric_var;

The magic is in &&myvar_&u - the double ampresand to get the is resolved as value of &myvar_1 etc. macro variable. If myvar was also macro variable, you'll need tripple ampresand &&&myvar_&u.

Also I skipped the data step, it' not needed.

Upvotes: 1

Related Questions