Search code examples
sasposition

SAS find first value in column and get row number


I'm trying to import and clean some file where the first rows of column 1 contains description and column names. the data begins after the row containing "BEGINDATA". Is it possible to search sas table for "BEGINDATA" and assign the row number to a macro variable? In the next data step I could use firstobs=macro variable to load only data.

Thanks for any hint!


Solution

  • Certainly! See this example.

    /* Generate example data */
    data have;
        do i = 1 to 10000;
            if(i = 100) then description = 'BEGINDATA';
                else call missing(description);
    
            value = rand('uniform');
            output;
        end;
    
        drop i;
    run;
    
    /* Get row where the data begins. Only keep the description variable 
       to reduce the size of the PDV */
    data _null_;
        set have(keep=description);
    
        if(description = 'BEGINDATA') then do;
            call symputx('startrow', _N_, 'G');
            stop;
        end;
    run;
    
    /* Read from the data start row */
    data want;
        set have(firstobs=&startrow.);
    run;