Question

I have tables on SAS with the configuration below , although with many more variables:

date    var12   var41
2013M8  -25.6   -30
2013M9  -24.5   -27.3
2013M10 -26.4   -25.7
2013M11 -32.6   -29.2
2013M12 -30.7   -27.3
2014M01 -29.2   -30.3

I'm trying to put them like this:

var12(t0) var12(t-1) var12(t-2)  var41(t0) var41(t-1) var41(t-2)
-29.2     -30.7     -32.6        -30.3     -27.3      -29.2
-30.7     -32.6     -26.4        -27.3     -29.2      -25.7
-32.6     -26.4     -24.5        -29.2     -25.7      -27.3
-26.4     -24.5     -25.6        -25.7     -27.3      -30

This is part of the autoregression , where there is a case transform. I've checked internet on how to do that but not with a stellar success so far.

Please not I'm not trying to do an auto regression , only transforming the data to apply a method later on.

Cheers.

Was it helpful?

Solution

A side by side merge does this efficiently. Basically you just take the same dataset 3 times, but two of those times chop off the first or first two rows with FIRSTOBS (so it starts at row 2 or row 3). You use rename so the variables don't overwrite each other, and there you go.

The merge without a BY statement is called a side-by-side merge, and just takes a row from each dataset, then moves onto the next row of each dataset.

data have;
input date  $  var12   var41;
datalines;
2013M8  -25.6   -30
2013M9  -24.5   -27.3
2013M10 -26.4   -25.7
2013M11 -32.6   -29.2
2013M12 -30.7   -27.3
2014M01 -29.2   -30.3
;;;;
run;

data want;
merge   have(rename=(var12=var12_t2 var41=var41_t2) in=h1) 
        have(firstobs=2 rename=(var12=var12_t1 var41=var41_t1) in=h2) 
        have(firstobs=3 rename=(var12=var12_t0 var41=var41_t0) in=h3);
        if h1 and h2 and h3;
run;

If you have a BY group (like an ID) you may need to manipulate it some to make that work.

OTHER TIPS

After some time on it, I did the following:

data have;
input date  $  var12   var41;
datalines;
2013M8  -25.6   -30
2013M9  -24.5   -27.3
2013M10 -26.4   -25.7
2013M11 -32.6   -29.2
2013M12 -30.7   -27.3
2014M01 -29.2   -30.3
;;;;
run;

/we change the date/

data a_want;
set have;
nouv_date=mdy(substr(date,6,2),01,substr(date,1,4));
format nouv_date monyy5.;
drop date;
run;

/ we do it for a1 to a6/

data a1; set a_want;
run;

/ we do it for a1 to a6/

data a6;
set a5;
proc sql;
delete from a6 where nouv_date=(select max(nouv_date) from a6);
quit;


data a; 
merge a1(rename=(var12=var12_t0 var41=var41_t_0)) 
a2(rename=(var12=var12_t_1 var41=var41_t_1)) 
a3(rename=(var12=var12_t_2 var41=var41_t_2)) 
a4(rename=(var12=var12_t_3 var41=var41_t_3)) 
a5(rename=(var12=var12_t_4 var41=var41_t_4))
a6(rename=(var12=var12_t_5 var41=var41_t_5)); 
by nouv_date;
run;

I wanted to put that, in case there will be people who will have a look. It is a bit different from what I've asked first hand but anyway. I hope it will help.

You can imagine as well that some part of it still has to be automated but it is a different part.

Maybe not the most elegant but I did piece by piece, with your help , especially you, Joe.

Cheers.

PS : I know it is not very efficient but long story

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top