I have a dataset with years 2000-2019 and one variable for each year with the state that reported the data (e.g., STATE_2000, STATE_2001, STATE_2002). I need to populate a variable with the reporting state for each month and year of the data instead of just by year. For example, if I have STATE_2000, I need one for each month like STATE_012000, STATE_022000, STATE_032000, etc. This will just be a copy of the STATE_2000 variable with a new name, 12 times over for each year. I am trying to find a way to succulently write the code instead of writing it all out like this because I have 20 years of data: data do_not_want; set have; STATE_012000=STATE_2000; STATE_022000=STATE_2000; STATE_032000=STATE_2000; run; I tried playing around with a macro for the month but I don't think it likes the underscore. I think I figured out a way to do it with a macro statement for the year but it's only outputting the last year. Here's an example with a couple of years of data: %macro state_yrs; %do yr=2000 %to 2002; data want; set have; STATE_01&yr=STATE_&yr; STATE_02&yr=STATE_&yr; STATE_03&yr=STATE_&yr; STATE_04&yr=STATE_&yr; STATE_05&yr=STATE_&yr; STATE_06&yr=STATE_&yr; STATE_07&yr=STATE_&yr; STATE_08&yr=STATE_&yr; STATE_09&yr=STATE_&yr; STATE_10&yr=STATE_&yr; STATE_11&yr=STATE_&yr; STATE_12&yr=STATE_&yr; run; %end; %mend state_yrs; %state_yrs; The output is giving me the variable name I want (STATE_MMYYYY), but in this example, it's only outputting the year 2002 and not 2000 or 2001. Thank you!
... View more