-1

I have the following code that works properly in my SAS program to subset needed dates to shift hourly data but I need to convert to a macro so that I can call it for multiple data sets. I have very little experience in macro programming so any help would be appreciated.

    %let yr_beg=2007;
    %let yr_end=2020;
    
    
    data DST_FMT(drop=year);
    
        attrib hlo length=$1
            start dst_start end format=mmddyy10.;
            fmtname="dst";
            type="N";
                do year="&yr_beg" to "&yr_end";
                    start= nwkdom(2, 1, 3, year)+1; 
                                                    
                    end= nwkdom(1, 1, 11, year);
                    dst_start= start - 1;           
                                                    
                    label='*';          
                    output;
                end;
            start=.;end=.;  
            hlo="O";                
            label='';               
            output;
    run;
    proc format cntlin=DST_FMT; run;
C McCown
  • 67
  • 1
  • 7
  • Provide some more detail, you probably don't need macro. Q: What aspects of the code above are going to vary ? If only `yr_beg` and `yr_end` definitely don't need macro. Create a single custom format once that covers a larger number of years, say 1960 to 2060 and your done. If the n'th occurence of the w'th weekday of the m'th month part is changing you _might_ need macro. – Richard Sep 01 '21 at 09:52
  • Creating this as a macro is a deliverable requirement for the overall program (per my supervisor) There will be three datasets that need to be passed through this adjustment before being written to a permanent library and updated on a monthly basis by a non-technical person so the more automated the better. – C McCown Sep 01 '21 at 13:21
  • The code you posted is not operating on any data at all. It is just generating a format. What data is it that you want to operate on? What are you doing to the data? – Tom Sep 01 '21 at 14:09
  • Show how you'd use this for three different scenarios and we can help you generalize it. https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md – Reeza Sep 01 '21 at 16:15
  • To make a program like this dynamic you first make sure you have a working program for your 'base case' and that it works as expected. Then you repeat it once or twice and track what you need to change each time. Then you replace that with macro, dosubl or BY group processing code. It varies a bit depending on the situation but the general idea is always the same. – Reeza Sep 01 '21 at 21:07

1 Answers1

0

Here's how you can convert your current program to a macro for calling. This is not needed though, usually a %INCLUDE would be more appropriate for something like this since it's usually only done once.

You also do not need the quotes around the macro variable.

%macro generate_date_format(yr_beg= , yr_end);
data DST_FMT(drop=year);
    
        attrib hlo length=$1
            start dst_start end format=mmddyy10.;
            fmtname="dst";
            type="N";
                do year=&yr_beg to &yr_end;
                    start= nwkdom(2, 1, 3, year)+1; 
                                                    
                    end= nwkdom(1, 1, 11, year);
                    dst_start= start - 1;           
                                                    
                    label='*';          
                    output;
                end;
            start=.;end=.;  
            hlo="O";                
            label='';               
            output;
    run;
    proc format cntlin=DST_FMT; run;

%mend;

Execute macro:

%generate_date_format(yr_beg = 2008, yr_end = 2021);

Macro tutorial references: https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Sample macros from documentation: https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716

Macro documentation: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/titlepage.htm

Reeza
  • 20,510
  • 4
  • 21
  • 38