BookmarkSubscribeRSS Feed
archibald
Obsidian | Level 7

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.

 

 

8 REPLIES 8
Astounding
PROC Star

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.

archibald
Obsidian | Level 7

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;

PGStats
Opal | Level 21

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.

PG
Pamela_JSRCC
Quartz | Level 8

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.  

 

 

archibald
Obsidian | Level 7

@Pamela_JSRCC thanks . will give it a try.

Reeza
Super User

You should post your code. 

 

Arrays in SAS are only a way to reference variables. They have no meaning otherwise, similar to other languages. 

 

 

PGStats
Opal | Level 21

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

PG
archibald
Obsidian | Level 7

@PGStats this another way of dealing with this issue. thanks

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1219 views
  • 0 likes
  • 5 in conversation