How to create an array with the same size as another array in SAS

I have a very simple question that probably has an obvious answer, but it eludes me. Maybe someone can help?

The question is ...

I define a period, say

%let analysis = y_1005 -- y_1143;

      

where y_1005 defines some variables from year 2010 5 to 2011 43 weeks. Then from this I define an array

array period(*) &analysis;

      

Now I would like to define a second array with the same size as the first array and I believe there is a clever way to do this, for example

array new_array(dim(period));

      

But that obviously doesn't work. I have tried different things, but I cannot get it to work. How can you call "dim (period)" in array creation or do it in some other way?

+3


source to share


3 answers


What about:

%let analysis = y_1005 - y_1143;

data test;
    array period(*) &analysis;
    call symput ("n_periods", dim(period));
run;

data test2;
    array new_array(&n_periods.);
run;

      



(See also my comment on the original post)

+5


source


I came up with a solution that solves the problem discovered by @stevepastelan, namely additional variables outside of weeks 52/53. If you store the start and end periods in separate macro variables and use WEEKU5. informat (for example, 10W05 for the 5th week of 2010), you can use the DATA NULL statement to calculate the number of weeks between two periods and create a macro list of variable names to use in the array. I also used PROC FORMAT to generate the required format for the year / week part of the variable name (i.e. 1005).

Hope it helps.



%let start=10W05;
%let end=11W43;

proc format;
picture yrwk low-high=%0y%0U (datatype=date);
run;

data _null_;
length varname $10 all_names $1000;
weeks=intck('week',input("&start.",weeku5.),input("&end.",weeku5.));
do i=0 to weeks;
    varname=cats("y_",put(intnx('week',input("&start.",weeku5.),i),yrwk.));
    call catx(' ',all_names,varname);
end;
call symput('weeks',weeks+1); /* start and end weeks are inclusive, so need to add 1 */
call symput('all_names',all_names);
run;

data x;
array period{*} &all_names.;
array new_array{&weeks.};
run;

      

+2


source


0


source







All Articles