0
votes

I have a SAS dataset with the following 13 fields:

base_price tax_month1-12

base price is the price of a product before taxes are paid and the tax_month is the tax percent needed to be collected for each month.

I want to create a new variable group tax_paid1-12 which is the product of the base_price and each of the tax months.

Is there an efficient way to do this in SAS without multiplying the fields 1 at a time? The number of months could vary in the future, so I do not want to hard code the number of fields in the variable group.

1

1 Answers

1
votes

Unfortunately you need addtional step to compute number of months.

You can use arrays to calculate tax_paid for each month.

data source;
    infile datalines;
    input base_price tax_month1 tax_month2 tax_month3;
datalines;
1 2 . 4
5 6 7 8
;
run;

data _null_;
    set source;
    array tax_month(*) tax_month:;
    call symputx('n', dim(tax_month));
    stop;
run;

data result;
    set source;

    array tax_month(&n) tax_month1-tax_month&n;
    array tax_paid(&n) tax_paid1-tax_paid&n;

    do i = 1 to dim(tax_month);
        tax_paid(i) = base_price * tax_month(i);
    end;

    drop i;
run;