<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Build a list of vars mentioned in a SAS program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581797#M165402</link>
    <description>&lt;P&gt;I would approach this as:&lt;/P&gt;
&lt;P&gt;Read the code so each token is tested a valid variable name (unless you made the choice of using the VALIDVARNAME=Any&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The compare that list to data set of interest .&lt;/P&gt;</description>
    <pubDate>Fri, 16 Aug 2019 17:37:29 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-08-16T17:37:29Z</dc:date>
    <item>
      <title>Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581752#M165380</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any useful ideas for me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 15:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581752#M165380</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2019-08-16T15:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581766#M165389</link>
      <description>I'd load the code into a dataset, parse all the words and knock that result against the dictionary.tables.</description>
      <pubDate>Fri, 16 Aug 2019 16:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581766#M165389</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-08-16T16:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581774#M165392</link>
      <description>&lt;P&gt;Another way is to have :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length STR $ 32767;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and use&amp;nbsp;&lt;/P&gt;
&lt;P&gt;STR = catx(' ,', STR, Var_name)&lt;/P&gt;
&lt;P&gt;to collect all variables of interest. STR will be holding Var-names separated by comma or any other delimiter you like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 16:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581774#M165392</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-08-16T16:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581789#M165398</link>
      <description>Variable shortcut lists will make this almost impossible to guarantee or if you have any macro code as well.</description>
      <pubDate>Fri, 16 Aug 2019 17:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581789#M165398</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-16T17:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581797#M165402</link>
      <description>&lt;P&gt;I would approach this as:&lt;/P&gt;
&lt;P&gt;Read the code so each token is tested a valid variable name (unless you made the choice of using the VALIDVARNAME=Any&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The compare that list to data set of interest .&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 17:37:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581797#M165402</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-16T17:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: Build a list of vars mentioned in a SAS program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581801#M165404</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37814"&gt;@Walternate&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to make some working code based on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Get a list of SAS program files;
%let datafolder = Y:\Diverse Windows\AdHocProd;
filename df pipe "dir ""&amp;amp;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 = "&amp;amp;datafolder"||'\'||substr(line,37);
		output;
		c + 1;
	end;
	if eod then call symputx('pgmcnt',c);
run;
%put &amp;amp;=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 &amp;amp;pgmcnt;

		data _null_; set pgmlist(firstobs=&amp;amp;i obs=&amp;amp;i);
			call symput('thispgm',trim(pgm));
		run;

		data pgmwords (keep=program word);
			infile "&amp;amp;thispgm" truncover;
			length program $255 word $32;
			program = "&amp;amp;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 17:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Build-a-list-of-vars-mentioned-in-a-SAS-program/m-p/581801#M165404</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-08-16T17:55:36Z</dc:date>
    </item>
  </channel>
</rss>

