Sentry Page Protection
Simple Macro [4-5]
Referencing Macro Variable II In the last few sections, we have seen how we can reference a macro variable in values. E.g.
Macro variable can also be referenced in variable names. E.g.
Example %let name = Alfred; data class; set sashelp.class; where name = "&name"; height_&name = height; keep name height_&name; run; In this example, we have created a new variable called height_&name. When executing the program, SAS will resolve the macro variable from HEIGHT_&NAME to HEIGHT_ALFRED. A brand-new variable called HEIGHT_ALFRED is created: Separating Macro Variable Referencing with Text In the above example, the variable name is specified as: E.g. HEIGHT_&NAME The macro variable &NAME is specified at the end of the variable name. Now, what if we want the &NAME to be at the beginning of the variable name? E.g. &NAME_HEIGHT Do you think this will work? Unfortunately, it won't. Let's look at an example. %let name = Alfred; data class; set sashelp.class; where name = "&name"; &name_height = height; keep name &name_height; run; The variable is specified as &NAME_HEIGHT: When running the program, we saw issues on the SAS log: There is one warning message: WARNING: Apparent symbolic reference NAME_HEIGHT not resolved. And one error message: ERROR 180-322: Statement is not valid or it is used out of proper order. So, what's the issue? When referencing the macro variable, we have: &NAME_HEIGHT SAS does not recognize that it is a combination of macro variable reference (&NAME) and some additional text (-HEIGHT). It treats the whole text (NAME_HEIGHT) as the macro variable reference! For example, SAS will look up the symbol table and check if there is a macro variable called NAME_HEIGHT. Since it cannot find it, it gives a warning message about macro variable reference not being resolved. Because of that, the new variable cannot be created. Using a Period to Separate Macro Variable Reference and Text In order to separate the macro variable reference (&NAME) with the text (_HEIGHT), we need to add a period between the two. Example %let name = Alfred; data class; set sashelp.class; where name = "&name"; &name._height = height; keep name &name._height; run; A period is added between &NAME and _HEIGHT: SAS will resolve the macro variable reference from &NAME._HEIGHT to ALFRED_HEIGHT. The ALFRED_HEIGHT variable is created: Exercise
Create a new data set called CLASS_JANE from the SASHelp.Class data set. The data set should have only one observation for Jane. Create three new variables called:
The text JANE in your program should be dynamically assigned using a macro variable. Need some help?
HINT: SOLUTION: %let name = Jane; data class_&NAME; &name._height = height; keep name &name._height &name._weight &name._age;
Fill out my online form.
|