BookmarkSubscribeRSS Feed
Walternate
Obsidian | Level 7

Hi,

 

I am trying to build a program the output of which would be a list of the variables that get mentioned in the SAS program.

 

I have a general sense of processing each line of the program and doing lookups, but I'm not sure how specifically to approach it and was hoping to get some good ideas from this group. 

 

I do have a list of variables that I'm specifically interested in looking for, so if that's easier to program than just trying to find general variable names, I can definitely do that. 

 

Does anyone have any useful ideas for me?

 

Thanks!

 

 

5 REPLIES 5
tomrvincent
Rhodochrosite | Level 12
I'd load the code into a dataset, parse all the words and knock that result against the dictionary.tables.
KachiM
Rhodochrosite | Level 12

Another way is to have :

 

length STR $ 32767;

 

and use 

STR = catx(' ,', STR, Var_name)

to collect all variables of interest. STR will be holding Var-names separated by comma or any other delimiter you like.

 

Reeza
Super User
Variable shortcut lists will make this almost impossible to guarantee or if you have any macro code as well.
ballardw
Super User

I would approach this as:

Read the code so each token is tested a valid variable name (unless you made the choice of using the VALIDVARNAME=Any  in which case you made the whole thing a lot more complicated) and record the line number , "word". The NVALID function will test if a string is valid as variable name.

 

 

 

The compare that list to data set of interest .

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Walternate 

 

I tried to make some working code based on @ballardw 's suggestions. Note that it will find explicit mentioned variables only, not shortcuts etc. There is room for many refinements, so take it as a proof of concept.

* Get a list of SAS program files;
%let datafolder = Y:\Diverse Windows\AdHocProd;
filename df pipe "dir ""&datafolder\*.sas""";
data pgmlist (drop=line c);
	length pgm $255;
	infile df truncover end=eod;
	input line $char255.;
	if substr(line,1,1) ne '' then do;
		pgm = "&datafolder"||'\'||substr(line,37);
		output;
		c + 1;
	end;
	if eod then call symputx('pgmcnt',c);
run;
%put &=pgmcnt;

* Delete previous results;
proc datasets lib=work nolist;
	delete allpgmwords;
quit;

* Read all files and split in words that might be variables;
%macro readfiles;
	%do i=1 %to &pgmcnt;

		data _null_; set pgmlist(firstobs=&i obs=&i);
			call symput('thispgm',trim(pgm));
		run;

		data pgmwords (keep=program word);
			infile "&thispgm" truncover;
			length program $255 word $32;
			program = "&thispgm";
			input line $char255.;
			line = translate(line,'                   ','()[]{};/\+-*?.,=');
			do i = 1 to countw(line);
			word = scan(line,i,' ');
			if nvalid(word,'v7') then output;
			end;
		run;

		proc append base=allpgmwords data=pgmwords;
		run;
	%end;
%mend readfiles;
%readfiles;

/* reduce to a distinct list */
proc sql;
	create table uniquepgmwords as 
		select distinct 
			program,
			word 
		from allpgmwords;
quit;

* Get list of interesting variables;
data reflist;
	input refvar $32.;
cards;
chargednumber
connectednumber
cpr
;
run;

* Find programs where these variables are mentioned;
proc sql;
	create table result as
		select 
			uniquepgmwords.program,
			uniquepgmwords.word as variable_found
		from uniquepgmwords, reflist
		where upcase(refvar) = upcase(word)
		order by program, word;
quit;

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 611 views
  • 0 likes
  • 6 in conversation