BookmarkSubscribeRSS Feed
slolay
Fluorite | Level 6

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

7 REPLIES 7
art297
Opal | Level 21

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"

slolay
Fluorite | Level 6

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

art297
Opal | Level 21

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?

slolay
Fluorite | Level 6

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

art297
Opal | Level 21

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.;

slolay
Fluorite | Level 6

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

slolay
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 950 views
  • 0 likes
  • 2 in conversation