Search code examples
sasexpandproc

sas Proc expand procedure


I am trying to calculate 3 months moving average of the following data by Product by country( I only have two country variables here). Is there a way to do so?

Here is the sales table I have:

Date    Product  Country   Sales
201101  Sofa     US        100
201102  Sofa     US        200
201103  Sofa     US        250
201104  Sofa     US        300
201101  Sofa     CA        250
201102  Sofa     CA        300
201103  Sofa     CA        250
201104  Sofa     CA        300
201101  Chair    US        300
201102  Chair    US        300
201103  Chair    US        300
201104  Chair    US        300
201101  Chair    CA        300
201102  Chair    CA        300
201103  Chair    CA        300
201104  Chair    CA        300

I tried something like the following, but moving average is only calculated by country. Is there a way I can have it calculated by country, by product? Any ideas will be appreciated. thanks:)

PROC SORT DATA=Sales;
BY Country Product Date;
RUN;

PROC EXPAND DATA=Sales out =ma;
By Country Product;
CONVERT Value=Value_ma/transformin=(setmiss 0) transformout=(movave 3);
run;

Solution

  • after my comment i tested a bit, i guess concating product and country gives the result you are looking for (i hope i still did not understood something wrong):

    data have;
    input Date $ Product $ Country $ Sales ; 
    datalines; 
    201101  Sofa     US        100 
    201102  Sofa     US        200 
    201103  Sofa     US        250 
    201104  Sofa     US        300 
    201101  Sofa     CA        250 
    201102  Sofa     CA        300 
    201103  Sofa     CA        250 
    201104  Sofa     CA        300 
    201101  Chair    US        300 
    201102  Chair    US        300 
    201103  Chair    US        300 
    201104  Chair    US        300 
    201101  Chair    CA        300 
    201102  Chair    CA        300 
    201103  Chair    CA        300 
    201104  Chair    CA        300 
    ; 
    run;
    
    data have ;
    set have; 
    copr=catx("_",Product,country); 
    run; 
    PROC SORT DATA=have; 
    BY copr Date; 
    RUN;
    
    PROC EXPAND DATA=have out =ma  ; 
     By copr; 
    CONVERT sales=average /    transformin=(setmiss 0) transformout=(movave 3); 
    run;
    
    proc print data=ma; 
     var date product country average; 
    where time > 1; 
    run;
    

    result:

    enter image description here