3

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?

2
  • 2
    Just an observation: array period(*) y_1005 - y_1143 includes a lot of extra variable names that you do not need, such as y_1053, y_1054, ..., y_1099, y_1100, ... Mar 28, 2012 at 14:50
  • Yes, I noticed that right after I posted the question. Since my variables are right after each other I put y_1005 -- y_1143 (I need to extend the period for other analyses, and there are 53 weeks in some years).
    – kbjerring
    Mar 29, 2012 at 7:31

3 Answers 3

5

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)

0
2

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;
2
  • I think the asker had his variables already created, and was using -- (double dash) to select them. Mar 30, 2012 at 15:07
  • 1
    True, I already had the variables and could select them with --. But looking ahead this way of automizing the "week" problem will come in handy. Thanks for sharing!
    – kbjerring
    Apr 1, 2012 at 18:52
0

Only an integer constant, range of numbers, or * (asterisk) can be used to dimension an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.