Hi,
I have 20 variables itinerary_cab_1_sc itinerary_cab_2_sc.....itinerary_cab_20_sc and I need to use them in a array, but the array only works when I rename these variables into something like cab_1 cab_2..... cab_20 . Is there a way to use the original variables without renaming them?
Thanks.
Absolutely, you can use arrays with the original variable names.
But if you want help fixing your program, you will need to show the program.
here you go
array cab_array{20} cab_1 - cab_20;
array tax_array{20} taxcode1- taxcode20;
do i=1 to 20;
taxcode {i}= substr(cab_array{i},1,5);
end;
You could probably use :
array cab_array{20} itinerary_cab_1_sc -- itinerary_cab_20_sc;
Note the two dashes. The shortcut means all the variables in the list from itinerary_cab_1_sc to itinerary_cab_20_sc.
You can create an array out of variables already defined, someting like
array c1-c20 itinerary_cab_1_s itinerary_cab_2_s ... itinerary_cab_19_s itinerary_cab_20_s;
Something like the following datastep can help you generate the variable list:
* Write list of variables to the log;
data _null;
name_list length 1024;
name_list = '';
do i=1 to 20;
name=cats('itinerary_cab_', put(i,2.), '_s');
name_list = catx(' ', name_list, name);
end;
put '(' name_list ')';
run;
I don't have SAS installed on my tablet, so the above code has not been tested.
@Pamela_JSRCC thanks . will give it a try.
You should post your code.
Arrays in SAS are only a way to reference variables. They have no meaning otherwise, similar to other languages.
Your variable names don't allow for some shortcuts but you can definitely use them in an array, as long as they are all numeric or all character. Use
array iti {20}
itinerary_cab_1_sc itinerary_cab_2_sc itinerary_cab_3_sc itinerary_cab_4_sc
itinerary_cab_5_sc itinerary_cab_6_sc itinerary_cab_7_sc itinerary_cab_8_sc
itinerary_cab_9_sc itinerary_cab_10_sc itinerary_cab_11_sc itinerary_cab_12_sc
itinerary_cab_13_sc itinerary_cab_14_sc itinerary_cab_15_sc itinerary_cab_16_sc
itinerary_cab_17_sc itinerary_cab_18_sc itinerary_cab_19_sc itinerary_cab_20_sc;
and saying iti{5} will be the same as saying itinerary_cab_5_sc
@PGStats this another way of dealing with this issue. thanks
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.