본문 바로가기
SAS/SAS Tip's

Merry Christmas and a Happy New Year

by 기서무나구물 2018. 12. 19.

포스팅 목차

    Merry Christmas and a Happy New Year

     

     

     

    We wish you a Merry Christmas;

    SAS Programming
    %let whole = 750 ; *whole note in milliseconds ;
    
    data _null_ ;
      retain ns 'CnC#DnD#EnFnF#GnG#AnA#Bn'
             nf 'CnDbDnEbEnFnGbGnAbAnBbBn';
      infile cards dlm = '|' dsd ;
      input ont: $10. @@ ;
      nt = put (substr (ont,2), $2.) ;
      md = put (scan   (ont,2), $4.) ;
      n  = 12 * input (ont, 1.) - 8.5 + (find (ns,nt) <> find (nf,nt)) / 2 ;
      f  = 27.5 * 2 ** ((n - 1) / 12) ;
      call sound (f, &whole * (1 + ^^ findc (md,'.') / 2) / input (md, 3.)) ;
    cards ;
    4Dn 4|4Gn 4|4Gn 8|4An 8|4Gn 8|4F# 8|4En 4|4Cn 4|4En 4|4An 4|4An 8|4Bn 8
    4An 8|4Gn 8|4F# 4|4Dn 4|4F# 4|4Bn 4|4Bn 8|5Cn 8|4Bn 8|4An 8|4Gn 4|4En 4
    4Dn 8|4Dn 8|4En 4|4An 4|4F# 4|4Gn 2|4Dn 4|4Gn 4|4Gn 4|4Gn 4|4F# 2|4F# 4
    4Gn 4|4F# 4|4En 4|4Dn 2|4An 4|4Bn 4|4An 8|4An 8|4Gn 8|4Gn 8|5Dn 4|4Dn 4
    4Dn 8|4Dn 8|4En 4|4An 4|4F# 4|4Gn 2
    4Dn 4|4Gn 4|4Gn 8|4An 8|4Gn 8|4F# 8|4En 4|4Cn 4|4En 4|4An 4|4An 8|4Bn 8
    4An 8|4Gn 8|4F# 4|4Dn 4|4F# 4|4Bn 4|4Bn 8|5Cn 8|4Bn 8|4An 8|4Gn 4|4En 4
    4Dn 8|4Dn 8|4En 4|4An 4|4F# 4|4Gn 2|4Dn 4|4Gn 4|4Gn 4|4Gn 4|4F# 2|4F# 4
    4Gn 4|4F# 4|4En 4|4Dn 2|4An 4|4Bn 4|4An 8|4An 8|4Gn 8|4Gn 8|5Dn 4|4Dn 4
    4Dn 8|4Dn 8|4En 4|4An 4|4F# 4|4Gn 2
    ;
    
    run ;

     

     

    * 기쁘다 구주 오셨네;

    SAS Programming

     

    data holidays;
      retain fmtname '@$holiday';
      length data $3;
      ratio = 1.05946309436;
      str1 ='A A#B C C#D D#E F F#G G#';
      str2='A BbCbB#DbD EbFbE#GbG Ab';
      o = 1;
      do i = 0 to 87;
        p = 55 * ratio**i;
            data = compress(substr(str1,mod(i,12)*2+1,2)||o);
        output;
            if data^=compress(substr(str2,mod(i,12)*2+1,2)||o) then do;
              data = compress(substr(str2,mod(i,12)*2+1,2)||o);
              output;
            end;
            if mod(i,12)=2 then o=o+1;
      end;
      rename data=start p=label;
      keep fmtname data p;
    run;
    proc format cntlin=holidays; run;
    %macro play(input);
    data _Null_;
      %let i=1;
      %do %while(%scan(&input,&i,%str( ))^=);
        %let note = %scan(&input,&i,%str( ));
            %let pitch = %upcase(%scan(&note,1,=));
            %let duration = %scan(&note,2,=);
        %let i = %eval‎(&i+1);
            %if &pitch=R %then
              call sleep((1/&duration)*750);
            %else
              call sound(input("&pitch",$holiday.),(1/&duration)*300);
        ;
      %end;
    run;
    %mend;
    %play(%str(
    C6=1 B5=1.5 A5=6 G5=1 R=2 F5=2 E5=1 D5=1 C5=1
    R=2 G5=2 A5=1 R=2 A5=2 B5=1 R=2 B5=2 C6=.33
    C6=2 C6=2 B5=2 A5=2 G5=2 G5=1.5 F5=4 E5=2
    C6=2 C6=2 B5=2 A5=2 G5=2 G5=1.5 F5=4 E5=2 E5=2
    E5=2 E5=2 E5=2 E5=4 F5=4 G5=1 R=4 F5=4 E5=4
    D5=2 D5=2 D5=2 D5=4 E5=4 F5=1 R=4 E5=4 D5=4
    C5=2 C6=1 A5=2 G5=1.5 F5=6 E5=2 F5=2 E5=1 D5=1 C5=1
    ));

     

     

    * 크리스마스 트리 프로그램 : 

    http://blogs.sas.com/content/iml/2013/12/18/christmas-tree-matrix/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+TheDoLoop+%28The+DO+Loop%29

     

     

    SAS Programming
    proc iml;
    /* define helper functions ROW and COL */
    start row(x);       /* return matrix m such that m[i,j] = i */
       return( repeat( T(1:nrow(x)), 1, ncol(x) ));
    finish;
    start col(x);       /* return matrix m such that m[i,j] = j */
       return( repeat(1:ncol(x), nrow(x)) );
    finish;
     
    /* parameters for the tree dimensions */
    h = 100; w = h+1; b = int(h/10);
    M = j(w, h, .);         /* initialize h x w matrix to missing */
    x = col(M);             /* column numbers */
    y = w + 1 - row(M);     /* reverse Y axis */
     
    /* define the leafy portion of the tree */
    TreeIdx = loc(y>b & y<=2*x & y<=-2*x+2*h); /* a triangle */
    M[TreeIdx] = 0;
     
    /* place lights randomly within tree */
    N = int(0.12*ncol(TreeIdx)); /* use 12% of tree area */
    call randseed(1225);
    do i = 1 to 3;               /* 3 colors */
       idx = sample(TreeIdx, N, "WOR"); /* sample without replacement */
       M[idx] = i;               
    end;
     
    /* define the trunk */
    width = int(b/2);
    TrunkIdx = loc( y<= b & abs(x-nrow(M)/2)<width );
    M[TrunkIdx] = 4;
     
    /* write matrix in "long form" to SAS data set */
    Row = row(M);                    /* row index */
    Col = col(M);                    /* col index */
    create Tree var {"Row" "Col" "M"};  append;  close Tree;
    quit;
    
    
    proc template;
    define statgraph HeatmapTree;
    dynamic _X _Y _Z;
    begingraph;
       discreteattrmap name="christmastree";
          value '.' / fillattrs=(color=WHITE);    /* background color */
          value '0' / fillattrs=(color=GREEN);    /* tree color       */
          value '1' / fillattrs=(color=RED);      /* ornament color 1 */
          value '2' / fillattrs=(color=BLUE);     /* ornament color 2 */
          value '3' / fillattrs=(color=YELLOW);   /* ornament color 3 */
          value '4' / fillattrs=(color=BROWN);    /* tree trunk color */
       enddiscreteattrmap;
       discreteattrvar attrvar=Alias var=_Z attrmap="christmastree";
       layout overlay / xaxisopts=(type=discrete display=none)
                        yaxisopts=(type=discrete display=none reverse=true);
          heatmapparm x=_X y=_Y colorgroup=Alias / 
                        xbinaxis=false ybinaxis=false primary=true;
       endlayout;
    endgraph;
    end;
    run;
     
    ods graphics / width=500 height=800;
    proc sgrender data=Tree template=HeatmapTree;
       dynamic _X="Col" _Y="Row" _Z="M";
    run;
    반응형

    댓글