{"id":79,"date":"2007-11-04T04:09:31","date_gmt":"2007-11-04T08:09:31","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/sas-macros-letting-sas-do-the-typing\/"},"modified":"2009-10-29T11:18:44","modified_gmt":"2009-10-29T15:18:44","slug":"sas-macros-letting-sas-do-the-typing","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/sas-macros-letting-sas-do-the-typing\/","title":{"rendered":"SAS Macros: Letting SAS Do the Typing"},"content":{"rendered":"

I’ve been meaning to write up a bit on using macros in SAS to complement my previous post on macro variables<\/a> for quite a while. Luckily Norwegian guy<\/a> reminded me about the pain of starting programming in SAS and provided me some motivation. So here’s my take on using macros in programming.<\/p>\r\n\r\n

So what is a macro? Macros are a part of SAS that look through your code before the normal part of SAS sees it and writes out your code for you based on a special syntax. If you’ve ever found yourself copying and pasting code then you’ve probably been in a situation well suited for macros. They’re also great if you need to perform different functions under different conditions. Once I learned macros, SAS seemed a lot more like a usable (although weird) programming language and tasks seemed to get a lot easier (except actually picking the statistical techniques to use).<\/p>\r\n\r\n

Probably the easiest way to see what macros do is an example. So say we once again have a data set of tree heights<\/p>\r\n\r\n[sas]data trees;\r\ninput name:$8. height;\r\ncards;\r\nMaple 123\r\nMaple 78\r\nMaple 90\r\nElm 155\r\nElm 65\r\nElm 90\r\nElm 120\r\nBirch 100\r\nBirch 30\r\nMaple 111\r\nrun;[\/sas]\r\n\r\n

I already talked about how to find and use the mean and standard deviation for the whole data set<\/a>. Now what if we wanted to standardize each species by its own seperate mean and deviation? We could cut and paste but once we get a few more species or want to change something later this really becomes a hassle. So this is where macros come in.<\/p>\r\n\r\n

The first thing to do is to calculate the mean and standard deviations for each species. We can use proc means<\/code> again to do this. Since we won’t be using the output I’ll add the noprint<\/code> option and since we only want the means for the individual species and not the whole dataset I’ll add the nway<\/code> option. The class name;<\/code> statement tells SAS to find the statistics seperately for each species and the output<\/code> line tells SAS to save the mean and deviation in a dataset called meansd<\/code>.<\/p>\r\n[sas]proc means data=trees nway noprint;\r\nclass name;\r\nvar height;\r\noutput out=meansd mean=meanheight std=sdheight;\r\nrun;[\/sas]\r\n\r\n

Now we just need to get the values from the meansd<\/code> dataset into macro variables. We’ll use the _NULL_ dataset and call symput<\/code> again to create macro variables. This time we need to create seperate macro variables for each species. Luckily SAS automatically numbers each observation in a dataset in a column called _N_<\/code>. Since each line of the dataset corresponds to a tree species, we can easily use this identifier to create the macro variables by using call symput('mean'||left(_N_), meanheight);<\/code>. The left()<\/code> and ()trim<\/code> functions (numeric variables have extra spaces to the left and string variables have spaces to the right) removes any unnecessary spaces and the ||<\/code> concatenates (connects) the text “mean” with the line number to give give mean1<\/code>, mean2<\/code>, etc.. I’ll do the same thing for standard deviation and tree name. Once the macro variables are created, there is still one problem remaining. We don’t know how many species there were or how many macro variables were created. Luckily SAS will make another column that indicates the last line of the dataset when it sees end=newcolumnname<\/code> following a set statement. Then we just need to check if SAS is on the last line and if so save the line number (_N_<\/code>) to know the number of species of trees.<\/p> \r\n\r\n[sas]data _NULL_;\r\nset meansd end=last;\r\ncall symput(‘mean’||left(_N_),meanheight);\r\ncall symput(‘sd’||left(_N_),sdheight);\r\ncall symput(‘name’||left(_N_),trim(name));\r\nif last then call symput(‘numspecies’,_N_);\r\nrun;[\/sas]\r\n\r\n

If you ever want to check what macro variables you have in your program, you can use %PUT _USER_;<\/code> to print them all to the log file. Or if you want to see every macro variable available (SAS has quite a few automatic ones like operating system and date) use %PUT _ALL_;<\/code>. Inserting %PUT _USER_;<\/code> here produces:<\/p>\r\n[sas]GLOBAL NUMSPECIES 3\r\nGLOBAL NAME1 Birch\r\nGLOBAL NAME2 Elm\r\nGLOBAL NAME3 Maple\r\nGLOBAL MEAN1 65\r\nGLOBAL MEAN2 107.5\r\nGLOBAL MEAN3 100.5\r\nGLOBAL SD1 49.497474683\r\nGLOBAL SD2 38.837267326\r\nGLOBAL SD3 20.273134933[\/sas]\r\n

Now we’ve set a lot of macro variables but we still haven’t created a real macro. In SAS, macros are started with %MACRO macroname;<\/code> and finished with %MEND;<\/code> (short for M[acro]END). %<\/code>‘s are used to indicate commands that the SAS macro facility will read and remove before normal SAS sees the code. Anything not with a % will be printed out by the macro facility. Macros don’t spit out their code for SAS until they’re are called using %macroname<\/code>.<\/p>\r\n\r\n

So I’ll call my macro treestandardizer<\/code> but you can call it whatever you want. I’m going to use a pretty simple and specific macro but if you were going to use this often and for different datasets you would want to program it better. The first thing to do is create the final<\/code> dataset and set it to the trees<\/code> dataset. Since we need to loop through each species of tree, we’ll need a %DO<\/code> loop. Everything between %DO<\/code> and %END<\/code> will be repeated while i<\/code> increments from 1 to the number of tree species. If you want to combine text and a macro variable to reference another macro variable, you use the double ampersand &&<\/code> in SAS. For example, we want to get the mean for species 1 by looking in the macro variable &mean1<\/code> so we use &&mean&i<\/code>. I think<\/em> the macro processing part of SAS ends up running through the code twice, the first time finding the &&<\/code> and replacing it with &<\/code> and the &i<\/code> and replacing it with 1<\/code> to leave &mean1<\/code> and the second time finding &mean1<\/code> and pasting in the appropriate value (65). So we’ll have the do loop write out a series of if<\/code> statements to check what the name of the tree is and use the appropriate mean and deviation. Note that when using a string macro variable like &nameX<\/code>, you need to surround it with double quotes (the macro processor doesn’t look inside single quotes) so SAS doesn’t think it is a variable name. <\/p>\r\n\r\n[sas]%MACRO treestandardizer;\r\ndata final;\r\nset trees;\r\n%DO i = 1 %TO &numspecies;\r\nif name=”&&name&i” then stheight=(height-&&mean&i)\/&&sd&i; \r\n%END;\r\nrun;\r\n%MEND;[\/sas]\r\n\r\n

The previous code prepared the macro but nothing actually happens until we call it using %treestandardizer<\/code>. Unlike almost everything else in SAS this line doesn’t have to end in a semicolon (although it’s pretty unlikely to hurt if you forget and add one). So to call the macro:<\/p>\r\n[sas]%treestandardizer[\/sas]\r\n\r\n

If you want to see what happens when you call a macro, you can have SAS print the code generated by the macro to the log file with the option option mprint;<\/code> (make sure to set it before actually calling the macro). In this case, it gives:<\/p>\r\n[sas]MPRINT(TREESTANDARDIZER): data final;\r\nMPRINT(TREESTANDARDIZER): set trees;\r\nMPRINT(TREESTANDARDIZER): if name=”Birch” then stheight=(height- 65)\/49.497474683;\r\nMPRINT(TREESTANDARDIZER): if name=”Elm” then stheight=(height- 107.5)\/38.837267326;\r\nMPRINT(TREESTANDARDIZER): if name=”Maple” then stheight=(height- 100.5)\/20.273134933;[\/sas]\r\n\r\n

So it worked and we now have the standardized heights in the stheight<\/code> column of the final<\/code> dataset. This particular example could be done a few different ways (the easiest and probably better way being to merge the meancv<\/code> dataset with the trees<\/code>) but I hope it gives a decent introduction to SAS macros. If you have any specific questions or something wasn’t clear, feel free to ask in a comment.<\/p> \r\n\r\n

Here is the SAS source code<\/a> if you don’t feel like copying and pasting.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"I’ve been meaning to write up a bit on using macros in SAS to complement my previous post on macro variables for quite a while. Luckily Norwegian guy reminded me about the pain of starting programming in SAS and provided me some motivation. So here’s my take on using macros in programming.","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":[214,294,185,215,51,709,163,367,55,52],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/79"}],"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=79"}],"version-history":[{"count":1,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":540,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/540"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}