使用SAS根据2个变量创建投资组合

时间:2017-08-04 11:10:22

标签: sorting sas stock portfolio

我实际上是SAS新手,希望在我的电子表格中的2个变量之间形成投资组合。

基本上,我有一个名为'Up'的excel文件,里面有变量,比如'month,company,BM,market cap usd) 我想每个月对我的数据进行排序:大小(降序)然后BM(降序)。我想根据P25,P50和P75创建4个尺寸的投资组合,第一个尺寸组合高于P75(每个月),依此类推。然后,对于每个尺寸组合,创建了'BM'功能的4个新组合,以及P25,P50和P75。

有人可以帮助我并向我显示SAS代码以及将其添加到现有“Up”文件的方式(工作表名称也称为“up”)

1 个答案:

答案 0 :(得分:0)

So I agree with the comment, this is not asked well. However, it is a common problem to solve and somewhat fun. So here goes:

First I'm going to just make up some data. Google search how to read Excel in SAS. It's easy.

1000 companies with a random SIZE and BM value.

data companies(drop=c);
format company $12.;
do c=1 to 1000;
    company = catt("C_",put(c,z4.));
    size = ceil(100*ranuni(1));
    BM = ceil(100*ranuni(1));
    output;
end;
run;

So I'm assuming you just want equal amounts in these 4 groups. You don't want to estimate percentiles based on a distribution or KDE. For this, PROC RANK works well.

proc rank data=companies out=companies descending groups=4;
var size;
ranks p_size;
run;

We now have a variable P_SIZE that is values 0,1,2,3 based on the descending order of SIZE.

Sort the portfolios by that P_SIZE value.

proc sort data=companies;
by p_size;
run;

Now run PROC RANK again, this time using a BY statement with P_SIZE, ranking on BM, and creating P_SIZE_BM.

proc rank data=companies out=companies descending groups=4;
var bm;
by p_size;
ranks p_size_bm;
run;

P_SIZE_BM now contains values 0,1,2,3 for EACH value of P_SIZE.

Sort the data and see how it comes out:

proc sort data=companies;
by p_size p_size_bm;
run;