BookmarkSubscribeRSS Feed
afsand
Calcite | Level 5

Hi Everyone,

 

I want to create a dynamic array with variable dimensions, but the code is not working.

Can you please help with this?

 

Data _null_ ;

set adherants ;

if _n_=1 then do;
array p_reserve_adh_path_ac_FR[&nombre_obs_IA.,&nb_scn.];
array p_reserve_adh_path_ss_FR[&nombre_obs_IA.,&nb_scn.];
end;

run;

 

Thanks

11 REPLIES 11
PaigeMiller
Diamond | Level 26

Not working? That's not enough information. Explain further, please. If there are errors in the log, show us the ENTIRE log for this data step (do not select parts to show us an not show us other parts).


Also, please provide the values of your macro variables.

--
Paige Miller
afsand
Calcite | Level 5

the code just run forever, I cannot see any errors or the log.

 

Here are the values of the two macro variables.

 

33 %put &nombre_obs_IA.;
13415
34 %put &nb_scn. ;
1000

PaigeMiller
Diamond | Level 26

SHOW US the log. All of it. Even if there are no obvious errors.

 

 

--
Paige Miller
ballardw
Super User

Show us where the values are set for the macro variables &nombre_obs_IA. are set &nb_scn AND what the actual values are.

Also, instead of using a DATA _NULL_ try using some actual data set so that you can see if the variables were created. _NULL_ does not create any data set so the variables only would exist while that data step is running.

 

ARRAY statements are declarative not executable.  The use of the "if _n_= 1" has no effect on the behavior of the array statements and as shown in that example is a completely useless condition.

EVERY record in a SAS data set will have the exact same number of variables. Period. There is no way to have a variable exist on only one row.

 

If you run this code you will see that there are 3 new variables added to set in the new data set JUNK and since there is no attempt to assign values they are all missing.

%let somedim=3;

data junk;
   set sashelp.class;
   array new (&somedim.);
run;

@afsand wrote:

Hi Everyone,

 

I want to create a dynamic array with variable dimensions, but the code is not working.

Can you please help with this?

 

Data _null_ ;

set adherants ;

if _n_=1 then do;
array p_reserve_adh_path_ac_FR[&nombre_obs_IA.,&nb_scn.];
array p_reserve_adh_path_ss_FR[&nombre_obs_IA.,&nb_scn.];
end;

run;

 

Thanks


 

 

afsand
Calcite | Level 5

Thank you for your reply, I understand more. I think an array is not what I need for what I am trying to do.

 

I am doing some calculations on a data step table and I also want to create an output table containing the results. I tried to create an array like below and output the results of the array but it seems not working. 

 

Do you suggest me to create a data step table for the outputs instead of an array? if yes, how to output the results only in the ouput data table? I have never worked on two SAS tables at the same time, how to ouput only on a table?

 

Data _null_ ;

set adherants ;

array p_reserve_adh_path_ac_FR[&nombre_obs_IA.,&nb_scn.];
array p_reserve_adh_path_ss_FR[&nombre_obs_IA.,&nb_scn.];

 

----some calculations in between

----some calculations in between

---some calculations in between

---some calculations in between

 

p_reserve_adh_path_ac_FR[x,y] = sum(p_reserve_adh_path_ac_FR[x,y],0) + (A_ECH + A_DECES + A_RETRA + A_gross_up - A_FRAIS_GAR + (A_FRAIS_ADM - A_MERDISPO)) * fct_tx_fwrd;

p_reserve_adh_path_ss_FR[x,y] = sum(p_reserve_adh_path_ss_FR[x,y],0) + (A_ECH + A_gross_up + A_DECES + A_RETRA - A_FRAIS_GAR) * fct_tx_fwrd;

run;

Tom
Super User Tom
Super User

A data _NULL_ step is not going to create any data.  If you want to make a dataset then give it a name (or don't use a name at all and SAS will generate one in the series DATA1, DATA2, ....)

 

But do NOT try to create a dataset with millions of variables.  Millions of observations is reasonable, but they should each have a reasonable number of variables.

 

An ARRAY in SAS is just a method for choosing which variable to access based on an index into a list of variables. 

 

But you could think of a SAS dataset as a two dimensional array.  The variables are the columns and the observations are the rows.

 

Or you could store an N-dimensional matrix in a dataset by using N+1 variables. One for each of the indexes and the extra one for the actual value.

 

Please explain what is the INPUT for this problem?  What is the OUTPUT that you want?  

mkeintz
PROC Star

So you want to create two arrays, each with 13,415,000 cells.  I just tried it with the program below, which didn't do any actual data processing.  Instead it took 60 seconds, and exited with a

 

FATAL: Insufficient memory to execute DATA step program. Aborted during the COMPILATION phase.
ERROR: The SAS System stopped processing this step because of insufficient memory.

message.

%let nombre_obs_IA=13415;
%let nb_scn=1000;


data _null_;
  array a[&nombre_obs_IA.,&nb_scn.] ;
  array b[&nombre_obs_IA.,&nb_scn.] ;
  stop;
run;

But SAS makes no objections when the arrays are defined as _temporary_:

 

    array a[&nombre_obs_IA.,&nb_scn.] _temporary_;
    array b[&nombre_obs_IA.,&nb_scn.] _temporary_;

Of course, for _temporary_ arrays, you have to be aware that values are retained across iterations of the DATA step.

 

I suppose, if your problem is a memory constraint, that you might be able to allocate more ram to the sas process.

 

But the real question is: 

   why do you need two matrices with 13,415,000 cells each?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
afsand
Calcite | Level 5
Thank you very much, it works with the temporary option. I am running 700,000 scenarios for my work and I need to save the results of my calculations in a new output table. it seems easier in my head to use an array, but I understand that it is not that efficient. You can I output some calculated new variables in a new SAS table? you can see my code below.

I want to create a table called output, that will be populated with calculated new variables.

Data _null_ ;

set adherants ;

array p_reserve_adh_path_ac_FR[&nombre_obs_IA.,&nb_scn.];
array p_reserve_adh_path_ss_FR[&nombre_obs_IA.,&nb_scn.];



----some calculations in between

----some calculations in between

---some calculations in between

---some calculations in between



p_reserve_adh_path_ac_FR[x,y] = sum(p_reserve_adh_path_ac_FR[x,y],0) + (A_ECH + A_DECES + A_RETRA + A_gross_up - A_FRAIS_GAR + (A_FRAIS_ADM - A_MERDISPO)) * fct_tx_fwrd;

p_reserve_adh_path_ss_FR[x,y] = sum(p_reserve_adh_path_ss_FR[x,y],0) + (A_ECH + A_gross_up + A_DECES + A_RETRA - A_FRAIS_GAR) * fct_tx_fwrd;

run;
mkeintz
PROC Star

But the problem with a temporary array is that its values are not part of the program data vector, and therefore will not be output to a dataset, unless you copy those values to a regular variable.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

Can you describe the logic of your program. Maybe it can be done without arrays.

What are the initial values in an array - are they values assigned from your input data?

Next code illustarates creating an output data:

data want(keep = <reqired list of variables>);
  set have;
... if <condition> then output want; run;

May be it is possible to do the work in several steps, using some temporary tables:

data temp1;
  set have;
       .... first step code ...
      keep <ID and calculated variables on output>;
run;

data want;
 set temp1;
       ... final calaulations ...
run;

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 11 replies
  • 2125 views
  • 0 likes
  • 7 in conversation