2

I want to achieve the same output but instead of harcoding each of the array-element use something like var1 - var10 but that would jump by 10 like decades.

data work.test(keep= statename pop_diff:);
    set sashelp.us_data(keep=STATENAME POPULATION:);

    array population_array {*} POPULATION_1910 -- POPULATION_2010;
    dimp = dim(population_array);
/* here and below something like:
array pop_diff_amount {10} pop_diff_amount_1920 -- pop_diff_amount_2010;*/  

    array pop_diff_amount {10}  pop_diff_amount_1920 pop_diff_amount_1930 
                                pop_diff_amount_1940 pop_diff_amount_1950 
                                pop_diff_amount_1960 pop_diff_amount_1970 
                                pop_diff_amount_1980 pop_diff_amount_1990 
                                pop_diff_amount_2000 pop_diff_amount_2010;

    array pop_diff_prcnt {10}   pop_diff_prcnt_1920 pop_diff_prcnt_1930 
                                pop_diff_prcnt_1940 pop_diff_prcnt_1950 
                                pop_diff_prcnt_1960 pop_diff_prcnt_1970 
                                pop_diff_prcnt_1980 pop_diff_prcnt_1990 
                                pop_diff_prcnt_2000 pop_diff_prcnt_2010;

    do i=1 to dim(population_array) - 1;
        pop_diff_amount{i} = population_array{i+1} - population_array{i};
        pop_diff_prcnt{i} = (population_array{i+1} / population_array{i} -1) * 100;
    end;

RUN;

I am still beginner in it therefore I am not sure is this possible or easy to achieve. Thanks!

Mateusz Konopelski
  • 732
  • 1
  • 13
  • 30

3 Answers3

2

Not automatic but not all that difficult either. First create a data set of the names then transpose and use an unexecuted set to bring in the names and then define arrays. Note how arrays are define using [*] and name: as you did with population_array.

data names;
   do type = 'Amount','Prcnt';
      do year=1920 to 2010 by 10;
         length _name_ $32;
         _name_ = catx('_','pop_diff',type,year);
         output;
         end;
      end;
   run;
proc print;
   run;
proc transpose data=names out=pop_diff(drop=_name_);
   var;
   run;
proc contents varnum;
   run;

data pop;
   set sashelp.us_data(keep=STATENAME POPULATION:);
   array population_array {*} POPULATION_1910 -- POPULATION_2010;

   if 0 then set pop_diff;
   array pop_diff_amount[*] pop_diff_amount:;
   array pop_diff_prcnt[*]   pop_diff_prcnt:;
   do i=1 to dim(population_array) - 1;
      pop_diff_amount{i} = population_array{i+1} - population_array{i};
      pop_diff_prcnt{i} = (population_array{i+1} / population_array{i} -1) * 100;
      end;
   run;
proc print data=pop;
   run;

enter image description here

enter image description here

data _null_
  • 7,457
  • 10
  • 13
2

SAS is automatically going to increment the array elements by 1. Here is an alternative solution that creates the variables using one extra step to create a set of macro variables that hold the desired variable names. Since you are basing them off of the variable POPULATION_<year>, we will simply grab the years from those variable names, create the variable names for the arrays that we want, and store them into a few macro variables.

proc sql noprint;
    select cats('pop_diff_amount_', scan(name, -1, '_') )
         , cats('pop_diff_prcnt_', scan(name, -1, '_') ) 
    into :pop_diff_amount_vars separated by ' '
       , :pop_diff_prcnt_vars separated by ' '
    from dictionary.columns
    where     libname = 'SASHELP'
          AND memname = 'US_DATA'
          AND upcase(name) LIKE 'POPULATION_%'
    ;
quit;

data work.test(keep= statename pop_diff:);
    set sashelp.us_data(keep=STATENAME POPULATION:);

    array population_array {*} POPULATION_1910 -- POPULATION_2010;
    dimp = dim(population_array);

    array pop_diff_amount {*}  &pop_diff_amount_vars.;

    array pop_diff_prcnt {*}   &pop_diff_prcnt_vars.;

    do i=1 to dim(population_array) - 1;
        pop_diff_amount{i} = population_array{i+1} - population_array{i};
        pop_diff_prcnt{i} = (population_array{i+1} / population_array{i} -1) * 100;
    end;

RUN;
Stu Sztukowski
  • 5,197
  • 1
  • 8
  • 18
1

Getting the data out of the meta data (create variable year) would make coding life easier.

proc transpose data=sashelp.us_data out=us_pop(rename=(col1=Population));
   by statename;
   var population_:;
   run;
data us_pop;
   set us_pop;
   by statename;
   year = input(scan(_name_,-1,'_'),4.);
   pop_diff_amount=dif(population);
   pop_diff_prcnt =(population/lag(population))-1;
   format pop_diff_prcnt percent10.2;
   if first.statename then call missing(of pop_diff_amount pop_diff_prcnt);
   drop _:;
   run;
proc print data=us_pop(obs=10);
   run;

enter image description here

data _null_
  • 7,457
  • 10
  • 13