DATA Step, Macro, Functions and more

Scanning Program For Words

Reply
Contributor
Posts: 51

Scanning Program For Words

Hi All

I need to scan program code looking for certain variables that are kept in the set statement.

I need to qc some code.  The program reads in some datasets using the set statement. Only some of the variables in each dataset are kept using the keep= statement for further processing in the code.

I have to make sure that each variable in the keep= statements is actually used in the code further down otherwise to remove the variable from the keep= statement.

Is there any way I could automate this process?

data out1;

     set inds1(keep= customer product cost)

          inds2(keep= customer age address);

  ....sas statements;

run;

so I would like to run code which will check to see that all the variables are used in the sas statements below)

any variables that are not used to list.

Thanks in advance

Steve

PROC Star
Posts: 7,363

Scanning Program For Words

One way would be to replace your RUN statements with RUN CANCEL statements, and then capture and parse the log looking for: the strings: "WARNING: The variable" and "in the DROP, KEEP, or RENAME list has never been referenced"

Contributor
Posts: 51

Scanning Program For Words

Hi art297

Tried this but think it doesn't really work for my purpose. I think I will only get the message "...var ....has never been referenced" when in a statement, but not in a dataset option ie Keep=

Wrote some code which may make it easier to see:

data test1;

    a=1;

    b=2;

    c=3;

    d=4;

run;

data test2;

    set test1(keep=a b c);

    *--- var a is used - OK;

    x=a;

    *--- var b is used - OK;

    y=b;

    *--- but var c is not used so there is no need to read it in.;

    *--- this is what i need to capture ie c;

run;

Cheers about the run cancel; statement..didn;t know about this.

Steve

PROC Star
Posts: 7,363

Scanning Program For Words

There are a number of occassions when one will still want to keep variables that are read in, but which are never addressed in one's code.  I would feel safer if the code also included a keep option in the file identified in the data statement.  Then, you would only have to parse the code for fields that are in the set options's keep statement, but neither in the data statement's keep option and never used in the code.

i.e., if you can narrow down the variables, the run cancel concept could be used to test the code by removing those variables, one by one, from the set statement's keep option.  Would that accomplish what you are trying to do?

Contributor
Posts: 51

Scanning Program For Words

The code I am working with does have a keep= statement in the data statement. It keeps vars which have been read in the set statement but are not processed but are required.

Was hoping to see if there was a way I could scan the program file with specific words..ie with the the vars in the keep= statement. Thought maybe a macro could be used whereby i copy and paste the keep= vars into it and sas scans the code to see if it encounters them. If I get a count of >1 then I know they are referenced elsewhere, either in the data statement or in the code after the set statement. If they are only referenced once ie i the actual set statement I just copied them from then would think I can safely say they are not needed.

Manually removing one var at a time and run cancel; is not a viable option at this moment.

Thanks

Steve

PROC Star
Posts: 7,363

Scanning Program For Words

I don't have a packaged program for you, but I think that the following contains all or most of the nuggets that you might need.  The code would have to be modified to match the layout of your own code, and your coding practices would have to be sufficiently uniform for any parsing of this type to work.  Hopefully, the following will at least provide you with a start:

data example;

  input a b c d;

  cards;

1 2 3 4

5 6 7 8

;

run;

/* program saved as c:\art\testcode.sas */

/*data test (keep=x y);*/

/*  set example (keep=a b c);*/

/*  x=a;*/

/*  y=b;*/

/*run;*/

filename partcode "c:\art\partcode.sas";

data _null_;

  length word $32 datakeep $50 setkeep $50;

  infile "c:\art\testcode.sas" lrecl=300 end=eof;

  file partcode;

  input;

  if index(upcase(_infile_),"DATA ") then do;

    x=index(upcase(_infile_),"KEEP=")+5;

    y=index(upcase(substr(_infile_,x)),")")+x-1;

    datakeep=substr(_infile_,x,y-x);

    call symput('datakeep',datakeep);

    put _infile_;

  end;

  else if index(upcase(_infile_),"SET ") then do;

    x=index(upcase(_infile_),"KEEP=")+5;

    y=index(upcase(substr(_infile_,x)),")")+x-1;

    setkeep=substr(_infile_,x,y-x);

    call symput('setkeep',setkeep);

    _infile_=substr(_infile_,1,x-6)||

          "obs=1 "||substr(_infile_,x-5);

    put _infile_;

  end;

  else if index(upcase(_infile_),"RUN;") then do;

    length names $300 name $32 type $3 length 3;

    do i=1 to 999 until(name=' ');

      call vnext(name,type,length);

      if not (substr(name,1,1) eq "_" or

             upcase(name) in ('WORD','DATAKEEP','SETKEEP','EOF',

                                       'NAMES','NAME','TYPE','LENGTH','I')) then

             names=catx(' ',names,name);

    end;

    call symput('varnames',names);

  end;

  else put _infile_;

run;

%put &datakeep.;

%put &setkeep.;

%put &varnames.;

Contributor
Posts: 51

Scanning Program For Words

Hi Art

Many many thanks for this and the work you put in.

Just in the office now so not had time to look at it. Will hopefully get back to you today or monday to let you now it goes.

Thanks
Steve

Contributor
Posts: 51

Scanning Program For Words

Hi Art

I've not been able to go through this code yet. Pretty busy at the new job for which this macro was for. Not too sure about some of the code and logic so will have to take a bit of time to go through it..when i can find the time.

Will get back soon

Cheers

Steve

Ask a Question
Discussion stats
  • 7 replies
  • 205 views
  • 0 likes
  • 2 in conversation