BookmarkSubscribeRSS Feed
hetuvio
Calcite | Level 5

Dears,

I want to use multiple arrays: reg1 to reg5 (5 régions according to dialing codes). And I want to use 1 main array named "regs" which would be an array of the arrays "reg1", ..., "reg5". Is it possible to created such "nested arrays"? I got some issues when running the following SAS code :

Thanks so much in advance for your input,

Best regards,

Violaine

*- creation of the input dataset (1 record per region according to dialing code) -* ;

data indictel ;

  format geogr3 $50. geogr3n z2. ;

  geogr3n=01 ; geogr3='Ile-de-France' ; output ;

  geogr3n=02 ; geogr3='North-West'    ; output ;

  geogr3n=03 ; geogr3='North-East'    ; output ;

  geogr3n=04 ; geogr3='South-East'    ; output ;

  geogr3n=05 ; geogr3='South-West'    ; output ;

run ;

*- creation of the input data (1 record per geographical region)

data indictel ;

  set indictel ;

  array regs(*) _char_ reg: ;

  array reg1{1} $50. _temporary_ ('ILE-DE-FRANCE') ;

  array reg2{5} $50. _temporary_ ('BASSE-NORMANDIE' 'BRETAGNE' 'CENTRE' 'HAUTE-NORMANDIE' 'PAYS-DE-LA-LOIRE') ;

  array reg3{7} $50. _temporary_ ('ALSACE' 'BOURGOGNE' 'CHAMPAGNE-ARDENNE' 'FRANCHE-COMTE' 'LORRAINE' 'NORD-PAS-DE-CALAIS' 'PICARDIE') ;

  array reg4{4} $50. _temporary_ ('AUVERGNE' 'LANGUEDOC-ROUSSILLON' 'PROVENCE-ALPES-COTE' 'RHONE-ALPES') ;

  array reg5{4} $50. _temporary_ ('AQUITAINE' 'LIMOUSIN' 'MIDI-PYRENEES' 'POITOU-CHARENTES') ;

  do z = 1 to 5 ;

    do k = 1 to dim(regs(z)) ;

      if geogr3n=&z then do ; geogr2 = regs(z)(k); output ; end ;

    end ;

  end ;

  drop k ;

run ;

https://www.google.fr/

https://www.google.fr/

14 REPLIES 14
gergely_batho
SAS Employee

No.

You can use multidimensional arrays or hash objects (associative array) instead.

Kurt_Bremser
Super User

You cannot have an arrray of arrays, only an array of vars.

But you can have multidimensional arrays.

Although I can see no sense in your overly complicated code. Just write a data step that generates your regions in geogr2;

data_null__
Jade | Level 19

"Back in the day" we used arrays of arrays all the time.  I don't think there is anything useful for arrays of arrays today, but it still works.

data _null_;
  
array a(i) a1-a3 (1:3);
   array b(i) b1-b3 (11:13);
   array x(j) a b;
   do over a;
      do over x;
         put i= j= x=;
         end;
     
end;
  
run;

i=
1 j=1 a1=1
i=
1 j=2 b1=11
i=
2 j=1 a2=2
i=
2 j=2 b2=12
i=
3 j=1 a3=3
i=
3 j=2 b3=13
data_null__
Jade | Level 19

It was not my intention to suggest my example has ANYTHING to do the the OP's problem.  I am still waiting for a proper statement of the OPs problem.

You cannot reference an implicitly sub-scripted array as you suggest "x(1)" so I don't know what your statement is indented to show.

hetuvio
Calcite | Level 5

Thanks a lot to all for your replies. I'll try to explain a bit more clearly:

At the end, I would like to have a dataset containing 1 record per region as follows:

sasregion.png

I wanted to start with a simple dataset including 2 variables GEOGR3 and GEOGR3N (and so, only 5 records - or "5 main régions").

Then, in order to get the dataset above (22 records): I wanted to use an array in order to avoid to repeat pièces of codes.

My example is probably not the best example. Actually, my code works when I combine arrays and macro code. But I wanted to share with you that example because I have often been faced to such problems including nested arrays. So, I wanted to know what I'm misunderstanding...

Thanks a lot,

Best regards

data_null__
Jade | Level 19

This works "sort of" (it does make the data you want) but because you can't get the DIM of the REGn arrays from the DIM function it's not good.

data indictel ;
   format geogr3 $50. geogr3n z2. ;
   geogr3n=01 ; geogr3='Ile-de-France' ; output ;
   geogr3n=02 ; geogr3='North-West'    ; output ;
   geogr3n=03 ; geogr3='North-East'    ; output ;
   geogr3n=04 ; geogr3='South-East'    ; output ;
   geogr3n=05 ; geogr3='South-West'    ; output ;
   run;
proc print;
  
run;

data indictel;
   set indictel;
   array reg1(i) $50. r_1_1         ('ILE-DE-FRANCE');
   array reg2(i) $50. r_2_1-r_2_5   ('BASSE-NORMANDIE' 'BRETAGNE' 'CENTRE' 'HAUTE-NORMANDIE' 'PAYS-DE-LA-LOIRE');
   array reg3(i) $50. r_3_1-r_3_7   ('ALSACE' 'BOURGOGNE' 'CHAMPAGNE-ARDENNE' 'FRANCHE-COMTE' 'LORRAINE' 'NORD-PAS-DE-CALAIS' 'PICARDIE') ;
   array reg4(i) $50. r_4_1-r_4_4   ('AUVERGNE' 'LANGUEDOC-ROUSSILLON' 'PROVENCE-ALPES-COTE' 'RHONE-ALPES') ;
   array reg5(i) $50. r_5_1-r_5_4   ('AQUITAINE' 'LIMOUSIN' 'MIDI-PYRENEES' 'POITOU-CHARENTES');
   array d(geogr3n) d1-d5 (1 5 7 4 4); /*this is the problem, d1=dim(reg1); d2=dim(reg2); ... */
  
array x(geogr3n) reg1-reg5;
   do i = 1 to d;
      geogr2 = x;
     
output;
     
end;
  
drop r_: d:;
   run;
proc print;
  
run;
Kurt_Bremser
Super User

Before going through all the hoops, just write a simple data step with datalines as wanted. Much easier to read and decipher in the future.

"When in doubt, use brute force" - Ken Thompson, creator of UNIX

data_null__
Jade | Level 19

Can you explain your problem using the HAVE/NEED scenario?

This is the data I have.

..........

This is the data I need.

..........

Haikuo
Onyx | Level 15

Well, this is probably another hidden 'features' that will never go to production.

Can you define Array of Array? Yes. Just like you can define Hash of Hash.

Can you put AOA into any practical application? Well, given the limitation it inherited, I doubt that. The limitation I learned is that it only supports implicit Arrays, which results in the only index usable is the implicit index "_i_", which leads to very limited applications, one being that all of the arrays involved has to be the same dimension.  

data test;

     input (v1-v4) (:$8.);

     cards;

a b c d

;

data _null_;

     set test;

     array a1 v1 v2;

     array a2 v3-v5;

     array aoa a1 a2;

     do over aoa;

           put a1= a2=;

     end;

run;

Haikuo

data_null__
Jade | Level 19

You just need to define index variable in ARRAY statement see my example.

When implicitly sub-scripted arrays were documented arrays of arrays was an example.

Haikuo
Onyx | Level 15

This is nice! First time to see this application. Pity my time wasted trying to figure it out. Thanks, John.

jakarman
Barite | Level 11

the concept of the SAS datastep (4- 5Gl) is seeing one record of tabular data-matrix.  The tabular data-matrix is the same concept as an array in in a 3Gl language like Pascal/Java. A major education attention point with programming in SAS is becoming aware and use that evolutionary difference.

What happens if you are using array-s with that is you are another dimension of array-s on top of that.

Sounds complicated?

In that case you have missed the fundamental concepts (PDV) of the datastep.

Having issues with code writing?

This is a dedicated science area with a lot of nasty difficult topics. Ready to go?

KISS keep it simple is by far the best way.

When needing extra code statements that is less serious issue as the unequal more complexity on avoiding duplications.
Code it brute force way.. Why is that a problem?    

---->-- ja karman --<-----

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
  • 14 replies
  • 1724 views
  • 0 likes
  • 6 in conversation