在Base SAS中使用频率桶创建表

时间:2017-04-12 11:57:26

标签: sas

以下是我的数据集示例:

  

城市日

     

亚特兰大10

     

坦帕95

     

亚特兰大100

     夏洛特20

     

Charlotte 31

     

Tampa 185

我想将“Days”分解为0-30,30-90,90-180,180 +的桶,这样“桶”沿着桌子的x轴,城市是沿y轴。

我尝试过使用PROC FREQ,但我没有SAS / STAT。在基础SAS中有没有办法做到这一点?

2 个答案:

答案 0 :(得分:0)

我相信这就是你想要的。这绝对是一股蛮力的"方法,但我认为它正确地概述了这个概念。

data have;
length city $9;
input city dayscount;
cards;
Atlanta 10 
Tampa 95
Atlanta 100
Charlotte 20
Charlotte 31
Tampa 185
;
run;

data want;
set have;
if dayscount >= 0 and dayscount <=30 then '0-30'n = dayscount;
if dayscount >= 30 and dayscount <=90 then '30-90'n = dayscount;
if dayscount >= 90 and dayscount <=180 then '90-180'n = dayscount;
if dayscount > 180 then '180+'n = dayscount;
drop dayscount;
run;

答案 1 :(得分:0)

解决此问题的方法之一是使用 Proc Format 分配值存储桶,然后使用 Proc Transpose 获取所需结果:

data city_day_split;
length city $12.;
input city dayscount;
cards;
atlanta 10 
tampa 95
atlanta 100
charlotte 20
charlotte 31
tampa 185
;
run;

/****Assigning the buckets****/
proc format;
value buckets
0 - <30 = '0-30'
30 - <90 = '30-90'
90 - <180 = '90-180'
180 - high = 'gte180'
;
run;

data city_day_split;
set city_day_split;
day_bucket = put(dayscount,buckets.);
run;

proc sort data=city_day_split out=city_day_split;
by city;
run;

/****Making the Buckets as columns, City as rows and daycount as Value****/
proc transpose data=city_day_split out=city_day_split_1(drop=_name_);
by city;
id day_bucket;
var dayscount;
run;  

我的输出:

> **city    |0-30  |90-180  |30-90  |GTE180**
> Atlanta   |10    |100     |.      |.
> Charlotte |20    |.       |31     |.
> Tampa     |.     |95      |.      |185