DATA Step, Macro, Functions and more

Referencing a variable name that depends on the value of a second variable

Reply
Occasional Contributor
Posts: 10

Referencing a variable name that depends on the value of a second variable

Hello,

I am having trouble figuring out how to use the value of a variable (in my case, the value of the variable "flight_counter" in the code below) to call up another variable to be changed (specifically, a variable with the prefix "ac_cat_", and a suffix dependent on "flight_counter"). I would eventually like to use a "do while" loop instead of the "do" loop in the code below, changing the variable "condition" at the end of each turn of the loop, such that the process is repeated until "condition" reaches 0. This will require "flight_counter" to increase an unknown number of times.So, for example, when flight_counter = 1, I need to insert the value of "n" into the variable "ac_cat_1" ; when flight_counter = 2, insert the value of "n" into "ac_cat_2" ; and so on. The set of variables prefixed "ac_cat_" is created, and filled with ".". I need a statement before the end of the "if" statement to populate the appropriate "ac_cat_" variable with the value of "n". For now, I have inserted the line "ac_cat_1 = n;", but the "1" needs to be changed to a reference to the value of "flight_counter".




data mydata_out; set mydata ;
flight_counter = 0;
empty_seat_counter = sgmt_empty_seats;
remaining_pass_counter = add_passenger_demand;
condition = remaining_pass_counter >= .25*empty_seat_counter;


if condition = 1 then do;
drop i ;
flight_counter = flight_counter + 1;
array cum_p {0:6} cum_p_0 - cum_p_6;
array lf {0:6} load_factor_0 - load_factor_6;
array choose{0:6};

do i = 0 to 6 ;
x = uniform(-1) ;
choose{i} = x >= cum_p{i} ;
end;
n = sum(of choose{*});

load_factor_chosen = lf{n} ;
ac_cat_1 = n;

end;

run;




I have tried to do this saving the value of "flight_counter" as a macro variable. For example:
%let f = flight_counter ;
ac_cat_&f = n;

However, this does not seem to be a valid way of referencing the variable "ac_cat_1" in this truncated loop; it simply resolves to "ac_cat_" in the log.

If someone could give me a tip on how to do this, I'd really appreciate it.

Many thanks.
SAS Super FREQ
Posts: 8,862

Re: Referencing a variable name that depends on the value of a second variable

Hi:
Example of using one variable (not the do loop iterator) as the index to an array was posted here:
http://support.sas.com/forums/thread.jspa?messageID=48471뵗

For example:
[pre]
array acc AC_CAT_1-AC_CAT_200;
n = some calc;
flight_number=1;
acc(flight_number) = n; /* however you calculate N */
[/pre]

Plus a second program example was also posted.

cynthia
Ask a Question
Discussion stats
  • 1 reply
  • 117 views
  • 0 likes
  • 2 in conversation