我正在尝试运行PROC TABULATE
来获取分散在类变量(称为brand
)中的各种变量的方法。我希望输出出来,以便每个指标和组合的组合一行。 brand
即可。这样我就可以将输出复制到Excel中并创建一个数据透视表以与用户共享。该表将是1列X,但生成许多行brand*metric
。
问题是SAS会生成带有合并表的嵌套单元格,所以我不能轻易地复制和粘贴输出 - 我必须手动清理一堆单元格。
我对SAS知之甚少,所以对任何帮助表示赞赏。谢谢!
答案 0 :(得分:2)
如果我明白你的意思,我想你可以使用PROC UNIVARIATE
。让我们使用SASHELP.CARS作为示例数据。我们可以将MODEL视为您的品牌,并获得MPG变量的平均值。
proc univariate noprint
data=sashelp.cars(where=(make=:'H'))
outtable=stats (keep=make _var_ _label_ _mean_)
;
class make ;
var mpg: ;
run;
结果
Obs Make _VAR_ _LABEL_ _MEAN_
1 Honda MPG_City MPG (City) 27.8235
2 Hummer MPG_City MPG (City) 10.0000
3 Hyundai MPG_City MPG (City) 23.0000
4 Honda MPG_Highway MPG (Highway) 34.0000
5 Hummer MPG_Highway MPG (Highway) 12.0000
6 Hyundai MPG_Highway MPG (Highway) 29.9167
注意:PROC UNIVARIATE不允许两个以上的类变量。如果您有两个以上的类变量,则可以对源数据进行排序,并使用BY
语句而不是CLASS
语句。
如果您需要多个类变量组合(MAKE MAKE * DRIVETRAIN等),那么您可能希望使用PROC SUMMARY。但是,您需要转置数据集以将变量转换为行而不是列。与PROC UNIVARIATE的结果相比,PROC SUMMARY在此简单输出格式中提供的统计数据要少得多。
%let class_list=make drivetrain ;
%let class_types=() make make*drivetrain ;
%let varlist = mpg: ;
proc summary chartype
data=sashelp.cars(where=(make=:'H'))
;
class &class_list ;
types &class_types ;
var &varlist ;
output out=stats(where=(_stat_='MEAN')) ;
run;
proc transpose data=stats out=want;
by _type_ &class_list ;
var &varlist ;
id _stat_;
run;
proc print; run;
结果
Drive
Obs _TYPE_ Make Train _NAME_ _LABEL_ MEAN
1 00 MPG_City MPG (City) 25.3000
2 00 MPG_Highway MPG (Highway) 31.6333
3 10 Honda MPG_City MPG (City) 27.8235
4 10 Honda MPG_Highway MPG (Highway) 34.0000
5 10 Hummer MPG_City MPG (City) 10.0000
6 10 Hummer MPG_Highway MPG (Highway) 12.0000
7 10 Hyundai MPG_City MPG (City) 23.0000
8 10 Hyundai MPG_Highway MPG (Highway) 29.9167
9 11 Honda All MPG_City MPG (City) 19.6667
10 11 Honda All MPG_Highway MPG (Highway) 23.6667
11 11 Honda Front MPG_City MPG (City) 30.3077
12 11 Honda Front MPG_Highway MPG (Highway) 37.0769
13 11 Honda Rear MPG_City MPG (City) 20.0000
14 11 Honda Rear MPG_Highway MPG (Highway) 25.0000
15 11 Hummer All MPG_City MPG (City) 10.0000
16 11 Hummer All MPG_Highway MPG (Highway) 12.0000
17 11 Hyundai Front MPG_City MPG (City) 23.0000
18 11 Hyundai Front MPG_Highway MPG (Highway) 29.9167
答案 1 :(得分:1)
最好的办法是使用import java.util.ArrayList;
public class Specials {
static public ArrayList<Integer> computeNSpecials(int N) {
ArrayList<Integer> specials = new ArrayList<>();
int abacus[] = new int[0]; // at index i we have the digit for 10^i
// This way, when we don't have enough specials,
// we simply reallocate the array and continue
while (specials.size() < N) {
// see if a carry operation is necessary
int currDigit = 0;
for (; currDigit < abacus.length && abacus[currDigit] == 9; currDigit++) {
abacus[currDigit] = 0; // a carry occurs when adding 1
}
if (currDigit == abacus.length) {
// a carry, but we don't have enough lines on the abacus
abacus = new int[abacus.length + 1];
abacus[currDigit] = 1; // we resolved the carry, all the digits below
// are 0
} else {
abacus[currDigit]++; // we resolve the carry (if there was one),
currDigit = 0; // now it's safe to continue incrementing at 10^0
}
// let's obtain the current number and the sum of the digits
int sumDigits = 0;
for (int i = 0; i<abacus.length; i++) {
sumDigits += abacus[i];
}
// is it special?
if (sumDigits % abacus.length == 0) {
// only now compute the number and collect it as special
int number = 0;
for (int i = abacus.length - 1; i >= 0; i--) {
number = 10 * number + abacus[i];
}
specials.add(number);
}
}
return specials;
}
static public void main(String[] args) {
ArrayList<Integer> specials=Specials.computeNSpecials(100);
for(int i=0; i<specials.size(); i++) {
System.out.println(specials.get(i));
}
}
}
将其放入表中,然后使用ods output
按照您的意愿制作。 proc transpose
会创造一些看起来不错的东西,不一定对你的特定用途有用。
PROC TABULATE
使用ods output table=tab_out;
proc tabulate data=sashelp.cars;
class make;
var mpg_city mpg_highway;
tables make*(mpg_city mpg_highway),mean;
run;
ods output close;
proc transpose data=tab_out out=tab_trans;
by make;
var mpg_:;
run;
如果你是9.3岁并且有PROC MEANS
选项可以跳过转置;没有它(在旧版本中)只需删除该选项,然后进行转置:
STACKODSOUTPUT