希望这对于能够帮助我的人来说是一个相对容易的问题。我是SAS(和编程)的新手。
我有一个数据集,其中包含许多变量,每个变量都用于测量在不同活动中花费的时间。 18,000个独特的条目。
我需要获得每个变量对总花费时间的贡献百分比。我知道如何做到这一点,而不是如何让SAS做到这一点。
以下是一些变量的屏幕截图以及最右侧的总数。如果您还有其他需要,请告诉我。
DomPazz和momo1644都提供了有用的解决方案,这些解决方案有助于我理解SAS。然而,这两个解决方案都是逐行地执行此操作,这表明我不清楚我实际上要实现的目标。我试图获得每个变量的总数,然后找出该总数对总体贡献的百分比。如果我是这样做的,那就是手工制作'它将是v1_total / overall-total * 100/1。
答案 0 :(得分:0)
您需要计算行总数,计算所有列总数然后驱动百分比。之前的答案仅计算行总数。
SAS代码:
inputFile = "path-to-input-file/a.txt"
outputFile = "path-to-output-file/b.txt"
stringToAPpend = "#"
with open(inputFile, 'r') as inFile, open(outputFile, 'w') as outFile:
for line in inFile:
outFile.write(stringToAPpend+line)
输出:
data have;
input surfm wskim Outdoorm dancem fishm snkrm tenpm Leisurem totadmin;
datalines;
0 0 0 0 0 0 0 0 180
0 0 0 0 0 0 0 0 180
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 98.75
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 30
0 0 0 0 0 0 0 0 30
0 0 0 0 120 0 0 120 750
0 0 0 0 0 0 0 0 30
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1291.25
0 0 0 0 0 0 0 0 370
0 0 0 15 0 0 0 15 160
;
RUN;
/*Calculate Total time per Row*/
data row_totals;
set have;
total= surfm + wskim + Outdoorm + dancem + fishm + snkrm + tenpm + Leisurem + totadmin;
run;
/*Calcualte Percentages*/
proc sql;
create table want as
select
sum(surfm)/sum(total) format=percent10.2 as surfm,
sum(wskim)/sum(total) format=percent10.2 as wskim,
sum(Outdoorm)/sum(total) format=percent10.2 as Outdoorm,
sum(dancem)/sum(total) format=percent10.2 as dancem,
sum(fishm)/sum(total) format=percent10.2 as fishm,
sum(snkrm)/sum(total) format=percent10.2 as snkrm,
sum(tenpm)/sum(total) format=percent10.2 as tenpm,
sum(Leisurem)/sum(total) format=percent10.2 as Leisurem,
sum(totadmin)/sum(total) format=percent10.2 as totadmin,
sum(total) as total
from row_totals
;
quit;