Assiging the same value for another column

223 views Asked by At

I have a task in SAS; What I need to do is to assign the October Current_value (60) in the column Fake_1 and let repeat it only for the months of October, November and December; the same logic should be applied to valorise column Fake_2, and assign the Current_Value of November both for November and December of columns Fake_2. That's how my table should look like:

Product_Code    Division    Category    Payment_Frequency   Selling_Type    expiring_month  Current_Value   Fake_1  Fake2
C611720         17822           AZ          Monthly             NSD             1               63      
C611720         17822           AZ          Monthly             NSD             2               72      
C611720         17822           AZ          Monthly             NSD             3               23      
C611720         17822           AZ          Monthly             NSD             4               24      
C611720         17822           AZ          Monthly             NSD             5               90      
C611720         17822           AZ          Monthly             NSD             6               87      
C611720         17822           AZ          Monthly             NSD             7               56      
C611720         17822           AZ          Monthly             NSD             8               43      
C611720         17822           AZ          Monthly             NSD             9               57      
C611720         17822           AZ          Monthly             NSD             10              60          60      
C611720         17822           AZ          Monthly             NSD             11              48          60      48
C611720         17822           AZ          Monthly             NSD             12              32          60      48

How can it be done? I think the solution would be using retain function, but I am struggling a bit as when I use it, sas prints me value of 48 and 32 for Nov/Dec values in the column fake1.

data sox_8;
set sox_7;
retain Fake_1;
if Expiring_Month > 9 then Fake_1 = Current_Value;
run;

Thank you for the support

1

There are 1 answers

0
Joe On

The > 9 means it updates for 10, 11, and 12. You want only to update on 10, so:

data sox_8;
  set sox_7;
  retain Fake_1;
  if Expiring_Month = 10 then Fake_1 = Current_Value;
run;

Now, you may want to extend your code like so:

data sox_8;
  set sox_7;
  array Fake[*] Fake_1-Fake_2;
  retain Fake_1-Fake_2;
  if Expiring_Month gt 9 and Expiring_Month lt 12 then 
    Fake[Expiring_Month-9] = Current_Value;
run;

This will update for both 10 and 11. Presumably you'll eventually have code that, for 2021 perhaps, would need to do this for more than two months - this allows you the beginning of a program that will automatically pick up what it needs (you'll want to also calculate Fake dimensions based on the month you run, probably with a macro variable).

%let curmonth=10;
data sox_8;
  set sox_7;
  array Fake[*] Fake_1-Fake_%sysevalf(12-&curmonth);
  retain Fake_:;
  if Expiring_Month ge &curmonth and Expiring_Month lt 12 then 
    Fake[Expiring_Month - &curmonth + 1] = Current_Value;
run;

Of course, please don't use Fake as a variable name in your real code!