/**********************************************************************/


data _null_;                     
   infile mytext;                
   file outfile;                 
                                 

                                 
run;                             


/**********************************************************************/


data _null_;           
   infile 'draft.text';
   file 'just.heads';  
   input line $char80.;
   put line $char80.;  
run;                   


/**********************************************************************/



line="The quick red fox jumped over the lazy brown dog.";
word3=scan(line,3);                                      



/**********************************************************************/


data _null_;                          
   infile 'draft.text';               
   file 'just.heads';                 
   input line $char80.;               
   if scan(line,1,' ') eq '.head' then
      put line $char80.;              
run;                                  


/**********************************************************************/


data _null_;                               
   infile flist length=ileg;               
   file nosignal;                          
   input line $char80.;                    
   if ileg gt 0 and substr(line,3,1) ne '*'
      then put line $char80.;              
run;                                       


/**********************************************************************/


data _null_;                                                
   infile 'draft.text';                                     
   file 'code.examples';                                    
   input line $char80.;                                     
   first=scan(line,1,' ');                                  
                                                            
      /* Check for 'begin' signal                         */
   if first='.bgc' then                                     
      do;                                                   
         put line $char80.;                                 
                                                            
            /* Check for 'end' signal                     */
         do until scan(line,1,' ')='.enc';                  
            input line $char80.;                            
            put line $char80.;                              
         end;                                               
      end;
   else delete;
run;


/**********************************************************************/


data _null_;                       
   infile 'draft.text' length=lvbl;
   file 'just.heads';              
   input line $varying80. lvbl;    
   put line $varying80. lvbl;      
run;                               


/**********************************************************************/


   /* Begin macro definition.                             */
%macro heads(inf=,outf=);                                   
   data _null_;                                             
      infile "&inf" length=lvbl;                            
      file "&outf";                                         
      input line $varying80. lvbl;                          
      put line $varying80. lvbl;                            
   run;                                                     
                                                            
   /* End macro definition.                               */
%mend heads;                                                
                                                            
   /* Invoke the macro with parameters.                   */
%heads(inf=draft.text,outf=just.heads)
 

/**********************************************************************/


   /* Begin macro definition.                             */
%macro cwind(listfile=,outfile=);                           
   /* Read a file of filenames, and assign                */
   /* each to a numbered macro variable.                  */
data _null_;                                                
   infile "&listfile" length=ileg end=last;                 
   input x $varying80. ileg;                                
   call symput('fn'||left(put(_n_,3.)),x);                  
   if last then                                             
      call symput('num',left(put(_n_,3.)));                 
run;                                                        
                                                            
   /* Use the number of files to control                  */
   /* the number of iterations                            */
%do index=1 %to #                                       
   data _null_;                                             
      file "&outfile" mod;                                  
      infile "&&fn&index" length=ileg;                      
      input line $varying80. ileg;                          
      if scan(line,1,' ')='\bslash h8d' then                
         do;                                                
            input line $varying80. ileg;                    
            put line $varying80. ileg;                      
         end;                                               
      else delete;                                          
   run;                                                     
   %end;                                                    
                                                            
   /* End macro definition.                               */
%mend cwind;                                                
                                                            
   /* Invoke the macro with parameters.                   */                                                            
%cwind(listfile=~/text/files,outfile=~/text/wind.list)


/**********************************************************************/


%macro splits(head=,firsth=,lasth=,inf=,outfix=);           
   filename inf "&inf";                                     
   %let outfindx=0;                                         
   %let its=%eval(1+(&lasth-&firsth));                      
                                                            
      /* Continue as long as the number of this           */
      /* iteration is less than the number of the last    */
      /* signal minus the number of the first signal.     */
   %do %while (&outfindx<&its);                             
                                                            
         /* Creates a series of numbered output files     */
      filename outf "&outfix&outfindx";                     
      data _null_;                                          
         infile inf length=ileg;                            
         file outf;                                         
                                                            
            /* FLAG and COUNT keep track of the signal    */
            /* relative to the first and last occurrence  */
            /* you want to copy to another file.          */
         retain flag count 0;                               
         input x $varying80. ileg;                          
         z=substr(x,1,4);                                   
                                                            
            /* FLAG=1 causes all nonheaders to go to file.*/
         if flag=1 then do;                                 
            if z ne "&head" then                            
               put x $varying80. ileg;                      
            else stop;                                      
         end;                                               
         else do;                                           
                                                            
               /* Count the signals until you reach the   */
               /* first one you want, then turn on FLAG.  */
            if z="&head" then do;                           
               count=count+1;                               
               if count=&firsth then do;                    
                  flag=1;                                   
                  put x $varying80. ileg;                   
               end;                                         
            end;                                            
         end;                                               
       run;                                                 
                                                            
          /* Increment both the outfile index and the     */
          /* first signal to write to file.               */
       %let outfindx=%eval(&outfindx+1);                    
       %let firsth=%eval(&firsth+1);                        
   %end;                                                    
%mend splits;                                               
                                                            
   /* Invoke the macro with parameters as an example.     */
%splits(head=.h2,firsth=4,lasth=15,inf=~/text/chap2,        
        outfix=~/text/syntax)                                                                                                                  


/**********************************************************************/


%macro specs(head=,phstart=,inf=,inmemfx=,                  
       outfx=,outmemfx=,nummems=);                          
   filename inf "&inf";                                     
   filename outf "&outfx";                                  
                                                            
   %let outfindx=1;                                         
                                                            
      /* Execute once for each member in a PDS.           */
   %do %while (&outfindx le &nummems);                      
      data _null_;                                          
                                                            
            /* Increment input and output suffix          */
            /* numbers so each iteration reads from       */
            /* and writes to a separate member.           */
         infile inf(&inmemfx&outfindx);                     
         file outf(&outmemfx&outfindx);                     
         retain hflag keysflag 0;                           
         attrib test testph length=$20;                     
         input x $80.;                                      
         level=substr(x,1,3);                               
            /* Flag testing goes in reverse order -       */
            /* the first check: if all flags are correct, */
            /* write to file or stop.                     */
         if keysflag=1 then do;                             
            if level ne "&head" then put x $80.;            
            else stop;                                       
         end;                                                
         else do;                                            
                                                             
               /* If heading correct, check for           */ 
               /* starting signal.                        */ 
            if hflag=1 then do;                              
                test=upcase(scan(x,2));                      
                if test="%upcase(&phstart)" then do;         
                   keysflag=1;                               
                   hflag=0;                                  
                   put x $80.;                               
                end;                                         
                else hflag=0;                                
            end;                                             
                                                             
               /* If heading prefix correct,              */ 
               /* check for right level.                  */ 
            if level="&head" and keysflag=0 then do;         
               legx=length(x);                               
               if legx le 3 then hflag=1;                    
               else do;                                      
                  test=scan(x,2);                            
                  if test="%upcase(&phstart)" then do;       
                     keysflag=1;                             
                     put x $80.;                             
                  end;                                       
               end;                                          
            end;                                             
         end;
      run;                                                        
                                                                  
      /* Increment the index.                             */      
      %let outfindx=%eval(&outfindx+1);                           
   %end;                                                          
%mend specs;                                                      
                                                                  
   /* Invoke the macro with parameters.                   */      
%specs(head=\bslash h2,phstart=specifications,inf=pub.sg606.procs,
       inmemfx=chap,outfx=saslpo.sg606.procs,outmemfx=chap,       
       nummems=43)                                                


/**********************************************************************/


%macro outline(infilx=,outfilx=);                           
                                                            
      /* Infile and outfile come in as parameters.        */
   filename inf "&infilx";                                  
   filename outf "&outfilx";                                
   data _null_;                                             

         /* Accommodate variable length records           */
         /* with LENGTH= option.                          */
      infile inf length=ileg;                               
      file outf;                                            
                                                            
         /* Set flags to zero to start out.               */
      retain znum flag 0;                                   
      input x $varying80. ileg;                             
                                                            
         /* Variable z contains the first three           */
         /* characters of each line that you check as     */
         /* a potential header signal.                    */
      z=substr(x,1,3);                                      
                                                            
         /* If variable z is a header signal, variable    */
         /* zcheck contains the header level.             */
      zcheck=substr(z,1,2);                                 
                                                            
         /* Flag testing goes in reverse order.           */
         /* FLAG is 1 if header is valid, so check for    */
         /* level and write to file.                      */
      if flag=1 then do;                                    
         select(znum);                                      
            when(1) put 'H1 ' x $varying80. ileg;           
            when(2) put 'H2  ' x $varying80. ileg;          
            when(3) put 'H3   ' x $varying80. ileg;         
            otherwise put '*****New Head, SURPRISE*****';   
         end;                                               
         flag=0;                                            
      end;
      else do;                                                          

            /* Check for correct header signal,           */            
            /* discarding false signals.                  */            
         if zcheck='\h' and z ne '\he' and z ne '\hr'
            then do;                                                    
            znum=input(substr(z,3,1),1.);                               
                                                                        
               /* Check for trailing data on potential    */            
               /* signal. Turn flag on if none.           */            
            if index(x,' ')> ileg then flag=1;                          
            else do;                                                    
                                                                        
                  /* Count back from blank to find level  */            
                  /* on signal with trailing data,        */            
                  /* extract level, and write to file.    */            
               blank=index(x,' ');                                      
               x=substr(x,blank,(ileg-blank));                          
               select(znum);                                            
                  when(1) put 'H1 ' x $varying80. ileg;                 
                  when(2) put 'H2  ' x $varying80. ileg;                
                  when(3) put 'H3   ' x $varying80. ileg;               
                  otherwise put '****New Head, SURPRISE****';           
               end;                                                     
            end;                                                        
         end;                                                           
      end;                                                              
   run;                                                                 
%mend outline;                                                          


/**********************************************************************/