2013-07-18 5 views
1
PRODUCT CODE Quantity 
A   1 100 
A   2 150 
A   3 50 
total product A 300 
B   1 10 
B   2 15 
B   3 5 
total product B 30 

J'ai effectué un rapport de proc et le produit de rupture après donne la quantité totale pour chaque produit. Comment puis-je calculer une colonne supplémentaire sur la droite pour calculer la quantité de produit en pourcentage basé sur le sous-total?Calculer les pourcentages dans un RAPPORT PROC

Répondre

1

SAS en a un bon exemple dans sa documentation, here. Je reproduis une partie de ceci avec quelques commentaires supplémentaires ci-dessous. Voir le lien pour les jeux de données et les formats initiaux (ou créez vous-même les bases).

proc report data=test nowd split="~" style(header)=[vjust=b]; 
    format question $myques. answer myyn.; 
    column question answer,(n pct cum) all; 
    /* Since n/pct/cum are nested under answer, they are columns 2,3,4 and 5,6,7 */ 
    /* and must be referred to as _c2_ _c3_ etc. rather than by name */ 
    /* in the OP example this may not be the case, if you have no across nesting */ 

    define question/group "Question"; 
    define answer/across "Answer"; 
    define pct/computed "Column~Percent" f=percent8.2; 
    define cum/computed "Cumulative~Column~Percent" f=percent8.2; 
    define all/computed "Total number~of answers"; 

    /* Sum total number of ANSWER=0 and ANSWER=1 */ 
    /* Here, _c2_ refers to the 2nd column; den0 and den1 store the sums for those. */ 
    /* compute before would be compute before <variable> if there is a variable to group by */ 
    compute before; 
     den0 = _c2_; 
     den1 = _c5_; 
    endcomp; 

    /* Calculate percentage */ 
    /* Here you divide the value by its denominator from before */ 
    compute pct; 
     _c3_ = _c2_/den0; 
     _c6_ = _c5_/den1; 
    endcomp; 

    /* This produces a summary total */ 
    compute all; 
     all = _c2_ + _c5_; 

     /* Calculate cumulative percent */ 
     temp0 + _c3_; 
     _c4_ = temp0; 
     temp1 + _c6_; 
     _c7_ = temp1; 
    endcomp; 
run; 
Questions connexes