如何在sas中使用nobs,以便它可用于查找百分比频率

时间:2015-05-26 12:15:46

标签: sas

我导入了一个数据文件。它有246个观察结果。通过使用nobs,如何在代码的最后一句中替换246来查找百分比?

proc import datafile='G:\Data file\Dec 2014.csv'                                                                  
out=out.datafile 
dbms=csv 
replace;

*SUMMARY TABLE IS MADE;                                                                                                 
proc summary data = out.datafile missing;                                                                                                               
class AGE sentiment;  *age, sentiment are two columns in datafile;                                                                                                                                   
var ;                                                                                                                                                    
output out = out.summ_age ;                                                                                                   
run;

*SORTING SUMMARY TABLE BY AGE;                                                                                                                           
proc sort data = out.summ_age;                                                                                                                           
by age;                                                                                                                                                  
run;                                                                                                                                                     

*TRANSPOSING THE SORTED DATA WITH AGE AS OBS SENTIMENT VALUES AS COLUMNS;                                                                                
PROC transpose data = out.summ_age out = out.hsi_age;                                                                                                    
by age;                                                                                                                                                  
id sentiment;                                                                                                                                            
var count;                                                                                                                                               
run;

data out.hsi_age;                                                                                                                                        
set out.hsi_age;
tot = d+s+i;
PERCENTAGE=round((tot/246)*100,0.01); /*PERCENTAGE PER GROUP*/                                                                                            
run;

3 个答案:

答案 0 :(得分:1)

如果您想将数据集中的观察数量转换为宏变量,那么您可以将此计数放入宏变量中 - 如您的情况:

proc sql ;
  select count(*) 
  into :N
  from out.datafile  ;
quit ;

然后您可以在最后一步中调用它:

data out.hsi_age;                                                                                                                                        
set out.hsi_age;
tot = d+s+i;
PERCENTAGE=round((tot/&N.)*100,0.01); /*PERCENTAGE PER GROUP*/                                                                                            
run;

答案 1 :(得分:1)

因此,您有错误和200%以及所有PROC SUMMARY的原因会产生超出您想要的数据。你想要跨越年龄和情绪,但实际上你得到:

  • 年龄
  • 情绪
  • 年龄*情绪
  • 所有数据(总计)

你需要问你真正想要的是什么。这确实给了我们一点奖励:你不必去寻找NOBS,你实际上可以从其中一条额外的线上得到它(与年龄没有交叉的情绪)。

假设你有sashelp.class并用性取代情绪,你可以这样做:

proc summary data = sashelp.class missing;                                                                                                               
class AGE sex;  *sex replaces sentiment here;      
types sex age*sex;  *we want the grand total for each separate sex, and crossed with age;
var ;                                                                                                                                                    
output out = summ_age ;                                                                                                   
run;

*SORTING SUMMARY TABLE BY AGE;                                                                                                                           
proc sort data = summ_age;                                                                                                                           by age;                                                                                                                                                  
run;                                                                                                                                                     

*TRANSPOSING THE SORTED DATA WITH AGE AS OBS SEX VALUES AS COLUMNS;                                                                                
PROC transpose data = summ_age out = hsi_age;                                                                                                    
by age;                                                                                                                                                  
id sex;                                                                                                                                            
var _freq_;                                                                                                                                               
run;

data hsi_age;                                                                                                                                        
set hsi_age;
retain grand_tot;
if _n_=1 then grand_tot=M+F;  *the first row has the null age values, grand total by sex;
else do;
    tot = sum(M,F);
    PERCENTAGE=round((tot/grand_tot)*100,0.01); /*PERCENTAGE PER GROUP*/                                                                                            
    output;
end;
run;

您还可以通过proc tabulate生成您正在寻找的内容,但这也可以。

答案 2 :(得分:1)

NOBS选项创建可以访问的常规变量,其方式与给定数据步骤中的任何其他变量相同(唯一的区别是此变量永远不会输出到目标数据集)。我想您可以通过以下方式实现您愿意做的事情:

data out.hsi_age;                                                                                                                                        
    set out.hsi_age nobs=my_nobs_var;
    tot = d+s+i;
    PERCENTAGE=round((tot/my_nobs_var)*100,0.01); /*PERCENTAGE PER GROUP*/                                                                                       
run;