BookmarkSubscribeRSS Feed
Ritz
Calcite | Level 5

Hello,

 

I need to write a SAS Code to be able read through another SAS code and get a list of all the variables used. Is that possible and if so then how?

Please suggest, thanks!

8 REPLIES 8
Reeza
Super User

Go to lexjansen.com and search for parsers. You should also take a look at PROC SCAPROC. 

 

If your programs have macros this is infinitely more difficult. 

Kurt_Bremser
Super User

Concur with @Reeza. As soon as you use macro elements, resolving those will become a nightmare.

And variables are often "used" in SAS by not using them in the code. A simple set statement will put all variables in a dataset into the PDV without any of them being explicitly named. So you would need to resolve the whole contents of the dataset to determine the variables present in the data step.

ballardw
Super User

Not too mention indirect references such as variable lists, especially ones like var:  _numeric_ _character_ and _all_

Ritz
Calcite | Level 5
So, if the sas code to be parsed has macros, then this is technically an impossible task?
ballardw
Super User

A partial solution may be to start from the other end: Get variables from the datasets of interest (dictionary.columns or sashelp.vcolumn)

Then search the code files for references to those specific variables.  Any comparison should be made in a case-insensitive manner as your programmers are likely to have ThisVarName, thisvarname, Thisvarname, ThisvarName and such in the code.

 

Note: I hope you don't have any variables that match SAS supplied functions or keywords such as DATE, MAX, MIN, MEAN and so forth as differentiating between Variable, Function, Proc name, and procedure options.

 

Or take a class on compiler construction in a computer science course to understand more of the issues surrounding parsing computer code.

Reeza
Super User

@ballardw wrote:

A partial solution may be to start from the other end: Get variables from the datasets of interest (dictionary.columns or sashelp.vcolumn)

Then search the code files for references to those specific variables.  Any comparison should be made in a case-insensitive manner as your programmers are likely to have ThisVarName, thisvarname, Thisvarname, ThisvarName and such in the code.

 

Note: I hope you don't have any variables that match SAS supplied functions or keywords such as DATE, MAX, MIN, MEAN and so forth as differentiating between Variable, Function, Proc name, and procedure options.

 

Or take a class on compiler construction in a computer science course to understand more of the issues surrounding parsing computer code.


I use max/min/mean all the time 😉

 

It's going to be an interesting exercise. It's nice in theory, try to determine what variables are being used. But this is basically where human knowledge of processes is important. Assuming that technology can solve a lack of documentation is a flawed idea. 

ChrisHemedinger
Community Manager

SAS has a procedure that can analyze SAS programs as you run them -- it's called PROC SCAPROC.

 

This procedure generates a detailed log file with data set names and variable names and attributes -- any data that the program reads and writes is captured.

 

If you use SAS Enterprise Guide, you can use Program->Analyze->Analyze for Program Flow to see an example of the detailed output.  Try it with a simple program, allow it to create a process flow, and then look at the Note item that the process adds to the flow.  Here's an excerpt:

 

    Data Sets (Count=2)
        SASHELP.CARS  Type=DATA  Field count=15  Access=INPUT  Mode=SEQUENTIAL
          Path='C:\Program Files\SASHome\SASFoundation\9.4\core\sashelp'
            Make  Type=CHARACTER  Length=13
            Model  Type=CHARACTER  Length=40
            Type  Type=CHARACTER  Length=8
            Origin  Type=CHARACTER  Length=6
            DriveTrain  Type=CHARACTER  Length=5
            MSRP  Type=NUMERIC  Length=8  Format=DOLLAR8.
            Invoice  Type=NUMERIC  Length=8  Format=DOLLAR8.
            EngineSize  Type=NUMERIC  Length=8  Label="Engine Size (L)"

Also check Sample 58047: Parse output from PROC SCAPROC to create a data set with inputs and outputs  -- a sample that provides an example program that can parse the output of SCAPROC.

 

This approach captures all data variables that are read and written, even if your program does not explicitly name them.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1062 views
  • 3 likes
  • 5 in conversation