Data visualization with SAS programming

Fun With SAS ODS Graphics: Betsy Ross Edition

Reply
Contributor tc
Contributor
Posts: 74

Fun With SAS ODS Graphics: Betsy Ross Edition

StarsAndStripes.gif

 

With the July 4th weekend upon us, thought I'd make-like-Betsy-Ross and knock out a short program to "stitch" an American flag using specs found on Wikipedia. Happy Fourth of July, everyone!

 

* Fun w/SAS ODS Graphics, July 4th Stars-and-Stripes Edition (U.S. Flag) 
  Based on specs from wikipedia.org/wiki/Flag_of_the_United_States;
  
data stars(keep=x y);                                 /* Generate x/y points for 50 stars */
do r=1 to 9;                                          /* 9 rows of stars */
  y=1-r*0.0538;                                       
  do x=0.0633*(1+mod(r-1,2)) to 11*0.0633 by 2*0.0633;/* Odd rows have 6 stars, even have 5 (offset by 1) */
    output;                                          
  end;
end;       
 
data stripes(keep=spolygon sx sy);                    /* Generate x/y points for 7 red stripes */
do stripe=1 to 13 by 2;                               /* Need seven rectangles (polygons) */
  spolygon+1;                                         /* Rectangle counter */
  sx=0;    sy=stripe*0.0769;     output;              /* Upper left x/y point */
  sx=1.92; sy=stripe*0.0769;     output;              /* Upper right x/y point */
  sx=1.92; sy=(stripe-1)*0.0769; output;              /* Lower right x/y point */
  sx=0;    sy=(stripe-1)*0.0769; output;              /* Lower left x/y point */
end;

data canton(keep=cpolygon cx cy);                     /* Generate x/y points for 1 canton (blue rectangle) */
cpolygon+1;                                           /* Rectangle counter */
cx=0;    cy=1;        output;                         /* Upper left x/y point */
cx=0.76; cy=1;        output;                         /* Upper right x/y point */
cx=0.76; cy=1-0.5385; output;                         /* Lower right x/y point */
cx=0;    cy=1-0.5385; output;                         /* Lower left x/y point */

data flag;                                            /* Combine 3 datasets used to plot U.S. flag into 1 */
set stars stripes canton;

ods graphics on / reset=index border=off imagefmt=gif /* Plot American flag */
                  antialias height=5in width=9.5in;  
proc sgplot data=flag noautolegend noborder pad=0;
symbolchar name=uniStar char='2605'x;                 /* Unicode value for 5-pointed star */
xaxis display=none offsetmin=0 offsetmax=0 values=(0 1.9);
yaxis display=none offsetmin=0 offsetmax=0 values=(.005 1);
polygon x=sx y=sy id=spolygon / fill fillattrs=(color=CXB22234) nooutline;  * Stripes ("Old Glory Red");
polygon x=cx y=cy id=cpolygon / fill fillattrs=(color=CX3C3B6E) nooutline;  * Canton ("Old Glory Blue");
scatter x=x y=y / markerattrs=(symbol=unistar color=CXFFFFFF size=38pt);    * Stars (White);

 

 

Highlighted
Grand Advisor
Posts: 9,567

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

@tc

Awesome !.

The flag of China is much more simpler to plot than America.

Someone could polot your own country 's flag ?

 

 

 

 

 

data have;
x=5;y=15;star=unicode('\u2605');output;star=' ';
x=10;y=11;star1=unicode('\u2605');output;star1=' ';
x=10;y=18;star2=unicode('\u2605');output;star2=' ';
x=12;y=13;star3=unicode('\u2605');output;star3=' ';
x=12;y=16;star4=unicode('\u2605');output;
run;


proc sgplot data=have aspect=0.6 noautolegend noborder;
styleattrs wallcolor=red;
text x=x y=y text=star/ textattrs=(color=yellow size=80pt) ;
text x=x y=y text=star1/ textattrs=(color=yellow size=40pt) rotate=60;
text x=x y=y text=star2/ textattrs=(color=yellow size=40pt ) rotate=60;
text x=x y=y text=star3/ textattrs=(color=yellow size=40pt ) ;
text x=x y=y text=star4/ textattrs=(color=yellow size=40pt) rotate=20;

xaxis display=none offsetmin=0 offsetmax=0 values=(0 30);
yaxis display=none offsetmin=0 offsetmax=0 values=(0 20);
run;

index.gif

Community Manager
Posts: 2,691

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

[ Edited ]

Love these @tc and @Ksharp! We'll end up with the United Nations via SGPLOT before you know it!

SAS Super FREQ
Posts: 1,078

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

Nicely done.  I like the use of the rotated star character in the flags.

Contributor tc
Contributor
Posts: 74

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

irishflag.png

 

Vertical bar chart-based Flag of IrelandMan Happy

 

* Fun w/SAS ODS Graphics, Flag of Ireland (en.wikipedia.org/wiki/Flag_of_Ireland);
  
data flag;                                       /* Generate x/y points for stripes */
y=1.5; do x=1 to 3; output; end;

ods listing gpath='/folders/myfolders';          /* Generate bar chart flag */
ods graphics on / reset antialias noborder width=6in height=3in imagename="irishflag";                  
proc sgplot data=flag noautolegend pad=0 aspect=.5 noborder nowall;
vbar x / response=y  colorresponse=x colormodel=(CX169B62 CXFFFFFF CXFF883E)
         barwidth=1 nooutline baselineattrs=(thickness=0);
xaxis display=none min=.5 max=3.5 offsetmin=.16667 offsetmax=.16667; 
yaxis display=none min=0 max=1.5 offsetmin=0 offsetmax=0;
New Contributor
Posts: 4

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

[ Edited ]

Although U.S. flag works, China flag does not work on my machine.

 

My SAS Current version: 9.04.01M3P062415.

 

img0.png

Grand Advisor
Posts: 9,567

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

I am using SAS University Edition .
Grand Advisor
Posts: 9,567

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

I also tried Chinese version SAS . and got this .

 

x.png

Grand Advisor
Posts: 9,567

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

if you are using English version SAS, you could change its encoding as utf-8 .

 

D:\SASHome\SASFoundation\9.4\sas.exe -CONFIG "D:\SASHome\SASFoundation\9.4\nls\u8\sasv9.cfg"

 

 

And I also got star .

 

x.png

Community Manager
Posts: 2,691

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

Check out this article from @RobPratt - optimizing the placement of stars on the flag.  In it, he tackles the mathematical solution for arranging 51 stars if we were to add Puerto Rico to our official state roll call.

 

51 stars

Contributor tc
Contributor
Posts: 74

Re: Fun With SAS ODS Graphics: Betsy Ross Edition

swedenflag.png

 

And, inspired by a Sunday morning visit to the Bolingbrook IKEA, heeere's the Flag of Sweden!

 

* Fun w/SAS ODS Graphics: Band plot Flag of Sweden (specs at wikipedia.org/wiki/Flag_of_Sweden);
  
data flag;                                                  /* Generate dummy x/y point */
retain x y -1; 

ods listing gpath='/folders/myfolders';                     /* Use band plots to generate flag image */
ods graphics on / reset antialias noborder width=8in height=5in imagename="swedenflag";                  
proc sgplot data=flag noautolegend pad=0 aspect=.625;       /* Proportion 5:8 = .625 */
styleattrs wallcolor=CX2D5FA1;                              /* 4055-R95B -> #2d5fa1 (Background) */
yaxis display=none min=0 max=10 offsetmin=0 offsetmax=0;    /* Colors: w3schools.com/colors/colors_ncs.asp */
xaxis display=none min=0 max=16 offsetmin=0 offsetmax=0;    
band x=x lower=4 upper=6 / fillattrs=(color=CXFFCA20);      /* 0580-Y10R -> #ffca20 (4:2:4 Vertical) */
band y=y lower=5 upper=7 / fillattrs=(color=CXFFCA20);      /* 0580-Y10R -> #ffca20 (5:2:9 Horizontal) */
Ask a Question
Discussion stats
  • 10 replies
  • 1504 views
  • 39 likes
  • 5 in conversation