DATA Step, Macro, Functions and more

Filling one set of vars from another set of vars

Frequent Contributor
Posts: 138

Filling one set of vars from another set of vars


I have a dataset with two sets of month variables. Each set has months going from 10 months before a target date to 10 months after.

ID     Target_date      mth_10_pre_a     mth_9_pre_a     mth_8_pre_a...mth_8_post_a      mth_9_post_a     mth_10_post_a mth_10_pre_b...etc.

1          2/2012                    3                         7                       .                    0                             3                    .                         .

What I want is to use values from the first set of month variables (a) to change some of the values in the second set of month variables from missing to 0; eg, since mth_10_pre_a has a value (or a 0), then mth_10_pre_b should be changed from missing to 0 (if mth_10_pre_b already had a positive value, however, it should remain unchanged).

I know that an array is theoretically the easiest way to do this, but since the numbers come in the middle of my variables, I'm not sure what's the most efficient way to go about it (ie, if there's a way to avoid changing all my variable names by hand).

Any help is much appreciated.

Super User
Posts: 10,466

Re: Filling one set of vars from another set of vars

You don't give us the whole list of variables but I think what you want is an array solution. No need to change any variable names just list in order. Note that with the first list in place you can copy-paste- search and replace _a with _b so not terrible. So you likely have 21 variables (and I would guess at some time in the future more)

Data want;

     set have;

     array prea mth_10_pre_a     mth_9_pre_a  mth_8_pre_a  ; /* continue to list all of the first group of variables just paste the names from any handy source*/

     array preb mth_10_pre_b     mth_9_pre_b  mth_8_pre_b ; /* again continue matching the order of the previous line*/

     do j= 1 to dim(prea);

          if prea ge 0 and missing (preb ) then preb = 0;



Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation