BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sophia_SAS
Obsidian | Level 7

Hi SAS Users,


I would like to write a macro that will keep my same pattern (color and value) across each value. I have already started a macro, but I can't seem to get it to work.


For example, using the code below and I would always like the color blue assigned when class=1.  As some graph do not class=1 value, I do not want the color blue then assigned to class=2.  Using this current code, for a dataset with values class=2 and class=3, the color assigned is red for both class values whereas it should be class=2 is red and class=3 is green.


Thanks!


data _null_;

   set have end=last;

   length color $20 value $5;      

select;

      when (class=1) do;

         color="Blue"; value="solid";  

       end;

      when (class=2) do;

         color="Red"; value="solid";

      end;

      when (class=3) do;

         color="Green"; value="solid";

      end;

      otherwise;

   end;   

if last then call symput('last',_n_);

call symput('pattern'||trim(left(put(_n_,8.))),'color=' || color || ' value=' || value ||';');                                      

run;     

%macro pattern;

   %do j=1 %to &last;    

   %end;

%mend pattern;

%pattern;

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

If you have SAS 9.3, this is a trivial problem. In SAS 9.3, you can define was is called an "attribute map" data set that you can use in the SG procedures (SGPLOT, SGPANEL, SGSCATTER). I wrote a couple of blog entries with examples of how you can use them. IN your case, you are creating bar charts, so you would use the FILLCOLOR column to set your colors and use SGPLOT or SGPANEL to create your bar charts. The nice thing is that you can reuse this dataset to accomplish what you wanted with your macro. Also, the dataset can contain MULTIPLE attribute maps that you can reference from the procedure by attrid.

http://blogs.sas.com/content/graphicallyspeaking/2012/02/27/roses-are-red-violets-are-blue/

http://blogs.sas.com/content/graphicallyspeaking/2012/03/01/roses-are-red-violets-are-blue-part-2/

Hope this helps!

Dan

View solution in original post

3 REPLIES 3
DanH_sas
SAS Super FREQ

If you have SAS 9.3, this is a trivial problem. In SAS 9.3, you can define was is called an "attribute map" data set that you can use in the SG procedures (SGPLOT, SGPANEL, SGSCATTER). I wrote a couple of blog entries with examples of how you can use them. IN your case, you are creating bar charts, so you would use the FILLCOLOR column to set your colors and use SGPLOT or SGPANEL to create your bar charts. The nice thing is that you can reuse this dataset to accomplish what you wanted with your macro. Also, the dataset can contain MULTIPLE attribute maps that you can reference from the procedure by attrid.

http://blogs.sas.com/content/graphicallyspeaking/2012/02/27/roses-are-red-violets-are-blue/

http://blogs.sas.com/content/graphicallyspeaking/2012/03/01/roses-are-red-violets-are-blue-part-2/

Hope this helps!

Dan

ArtC
Rhodochrosite | Level 12

If you can go with the newer SG procedures then follow Dan's suggestions, but take a look at this paper as well.

http://www.lexjansen.com/wuss/2012/142.pdf

However if as your question suggests, you are not using the the SG procedures, then take a look at this paper.

http://www2.sas.com/proceedings/sugi29/086-29.pdf

Although these two papers are directed at GPLOT and SGPLOT the concept with the legends, symbols, and patterns are similar.

If these do not clear up things for you come back and reframe your question.  There are also Macro issues in your code that may then need to be addressed.

sophia_SAS
Obsidian | Level 7

I did end up trying it both ways - once using just proc gchart and creating dummy variables and a code supplied by a paper provided by ArtC http://www2.sas.com/proceedings/sugi29/086-29.pdf.

I also then used the newer SG procedures and found Dan's suggestions and supplied papers helpful.  I guess I should start the transition to using the newer SG procedures.

Thank you both for your help.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1294 views
  • 3 likes
  • 3 in conversation