kbjerring
kbjerring

Reputation: 623

how to create array with same dimension as another array in SAS

I have a very simple question to which there is probably an obvious answer,... but it keeps eluding me. Maybe someone can help?

The question is this...

I define a period, say

%let analysis = y_1005 -- y_1143;

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

array period(*) &analysis;

Now I would like to define a second array with the same dimension as the first array, and I figure there is a smart way to do that, such as

array new_array(dim(period));

But this obviously not work. I have tried various things, but I can't get it to work. How is it possible to call "dim(period)" within the creation of an array, or to do this some other way?

Upvotes: 3

Views: 3256

Answers (3)

Longfish
Longfish

Reputation: 7602

I've come up with a solution that solves the problem spotted by @stevepastelan, namely the extra variables beyond weeks 52/53. If you store your start and end periods in separate macro variables and use the WEEKU5. informat (e.g. 10W05 for 2010 week 5), then you can use a DATA NULL statement to calculate the number of weeks between the 2 periods and create a macro list of the variable names to use in the array. I've also used PROC FORMAT to create the required format for the year/week part of the variable name (i.e. 1005).

Hope this 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;

Upvotes: 2

stevepastelan
stevepastelan

Reputation: 1304

How 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 to your original post)

Upvotes: 5

Related Questions