top 20 of programming languages<\/a> so I guess it’s going strong.<\/p>\r\n\r\nSo SAS is very good at working on rows of data but not quite so good at working with columns. For example if you have a column of tree heights and want to standardize by the mean and standard deviation, there is no easy way to do this without using macros or manually getting the mean and entering it into your program (this works the first couple times but it gets old quickly trust me).<\/p>\r\n\r\n
Macros seem to me to be sort of a patch that sits on top of the main SAS program to allow this sort of thing. Macros work by scanning through your program for special commands and replacing things before sending it onto the real SAS program. In effect, the macro processor writes your program for you.<\/p>\r\n\r\n
So back to our example of trees. Here is some data:<\/p>\r\n[sas]data trees;\r\ninput name $1-8 height 9-11;\r\ncards;\r\nMaple 123\r\nOak 78\r\nBirch 90\r\nElm 155\r\nPoplar 65\r\nrun;[\/sas]\r\n
I never actually use the cards<\/code> statements in real code but it’s handy for portable examples. Also the spaces between the two columns are significant since we told SAS to look the 1st through 8th column ($1-8<\/code>) for tree name<\/p>\r\nSo we now need to find the mean and s.d. of the heights:<\/p>\r\n[sas]proc means data=trees mean std;\r\nvar height;\r\noutput out=meansd mean=meanheight std=sdheight;\r\nrun;\r\n[\/sas]\r\n
Now our mean is stored in the meanheight<\/code> and sdheight<\/code> columns of the meantrees<\/code> dataset. So to get it into the macro part of SAS we need to use call symput(anyname,column)<\/code>. You can only use call symput<\/code> in data statements. This is a little silly since we don’t actually want to do anything with the data but I don’t make the rules. Luckily, SAS does provide the _NULL_<\/code> dataset which just dumps whatever you put in it after you finish the data step. SAS variables often have extra spaces attached so it’s probably smart to stick a trim(left(column))<\/code> in the call symput<\/code>.<\/p>\r\n[sas]data _null_;\r\nset meansd;\r\ncall symput(‘treemean’,trim(left(meanheight)));\r\ncall symput(‘treesd’,trim(left(sdheight)));\r\nrun;\r\n[\/sas]\r\nSo after call symput<\/code> we can access the stored values with &treemean<\/code> and &treesd<\/code>. They’re called macro variables by the way. If you ever need to check the contents of your macro variables you can use %PUT _users_;<\/code> to put them all into the log file. So now we can make a column of our standardized tree heights:<\/p>\r\n[sas]data trees;\r\nset trees;\r\nstandardized=(height-&treemean)\/&treesd;\r\nrun;[\/sas]\r\nNow the trees<\/code> dataset contains the standardized tree heights. This is a lot easier than looking in the results and entering it manually every time something changes.<\/p>","protected":false},"excerpt":{"rendered":"I just thought I’d do a quick post about SAS macro variables. They’re a pretty important part of SAS but when I had just started people told me not to worry about them and to just enter values by hand. After I finally got tired of constantly looking up results and entering them into programs […]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12,11],"tags":[54,561,30,599,51,53,709,56,55,52],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}