DATA Step, Macro, Functions and more

array and trim

Reply
Contributor
Posts: 62

array and trim

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.

 

 

Super User
Posts: 5,511

Re: array and trim

Posted in reply to archibald

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.

Contributor
Posts: 62

Re: array and trim

Posted in reply to Astounding

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;

Respected Advisor
Posts: 4,925

Re: array and trim

Posted in reply to archibald

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
Contributor
Posts: 39

Re: array and trim

Posted in reply to archibald

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.  

 

 

Contributor
Posts: 62

Re: array and trim

Posted in reply to Pamela_JSRCC

@Pamela_JSRCC thanks . will give it a try.

Super User
Posts: 19,815

Re: array and trim

Posted in reply to archibald

You should post your code. 

 

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

 

 

Respected Advisor
Posts: 4,925

Re: array and trim

Posted in reply to archibald

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
Contributor
Posts: 62

Re: array and trim

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

Ask a Question
Discussion stats
  • 8 replies
  • 352 views
  • 0 likes
  • 5 in conversation