<?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: Break up string using count function and how to loop through it in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453616#M114647</link>
    <description>&lt;P&gt;I would love to do that, but I have to follow what my supervisor is requesting.&amp;nbsp; I'm not sure his thinking on this.&amp;nbsp; He may think that since it is an oracle DB that we should use Oracle export process to include everything.&amp;nbsp; He may not think SAS can return everything we may need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Thu, 12 Apr 2018 15:36:27 GMT</pubDate>
    <dc:creator>jerry898969</dc:creator>
    <dc:date>2018-04-12T15:36:27Z</dc:date>
    <item>
      <title>Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453544#M114622</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to take this sql script string and break it up into column rows to start.&amp;nbsp; We are scripting out the sql from oracle but we want to create scripts that are formatted a specific way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CREATE TABLE "TEMP" ("TEMPID" NUMBER(7,0), "TESTID" NUMBER(6,0), "TYPE" VARCHAR2(5), "STARTDT" DATE, "SOURCE" VARCHAR2(5), "TITLE" VARCHAR2(6), CONSTRAINT "TEMP_PK" PRIMARY KEY ("TEMPID") ENABLE,&amp;nbsp; CONSTRAINT "TEMP_FK" FOREIGN KEY ("KEYID") REFERENCES "KEY" ("ID") ENABLE) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;TABNm COLNm COLType&lt;/P&gt;
&lt;P&gt;TEMP TEMPID NUMBER(7,0)&lt;/P&gt;
&lt;P&gt;TEMP TESTID NUMBER(6,0)&lt;/P&gt;
&lt;P&gt;TEMP TYPE VARCHAR2(5)&lt;/P&gt;
&lt;P&gt;TEMP STARTDT DATE&lt;/P&gt;
&lt;P&gt;TEMP SOURCE VARCHAR2(5)&lt;/P&gt;
&lt;P&gt;TEMP TITLE VARCHAR2(6)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I have so far.&amp;nbsp; I try to isolate just the variables and then take a count of double quotes and divide it by 2 to get the number of variables, but I'm not sure how to move each variable into a separate row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data x ;
	set raw ;
	if f1=1 then do ;
		tabname=scan(line,2,'"') ;
		ParenPos=find(line,'(') ;		
		x=substr(line,ParenPos) ;
		EndPos=find(x,'CONSTRAINT') ;
		if EndPos = 0 then EndPos=find(x,';') ;
		y=substr(x,1,EndPos-1) ;
		VarCnt=count(y,'"')/2 ;
	end ;	
run ;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 13:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453544#M114622</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2018-04-12T13:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453552#M114625</link>
      <description>&lt;P&gt;Not sure why you would be wanting to parse a string?&amp;nbsp; Why not just extract the metadata from the database?&amp;nbsp; That would be more robust.&lt;/P&gt;
&lt;P&gt;E.g.&lt;/P&gt;
&lt;PRE&gt;select * from dictionary.columns where ...;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Apr 2018 13:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453552#M114625</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-12T13:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453616#M114647</link>
      <description>&lt;P&gt;I would love to do that, but I have to follow what my supervisor is requesting.&amp;nbsp; I'm not sure his thinking on this.&amp;nbsp; He may think that since it is an oracle DB that we should use Oracle export process to include everything.&amp;nbsp; He may not think SAS can return everything we may need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 15:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453616#M114647</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2018-04-12T15:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453623#M114651</link>
      <description>&lt;P&gt;It doesn't matter if you use the database or SAS, any structured language has metadata about its tables.&amp;nbsp; In the DB these are held under dictionary tables, in SAS they are under sashelp.vtable and vcolumn (although you can refer to dictionary also in SQL through SAS).&amp;nbsp; So in either circumstance you should be able to pull metadata without resorting to code parsing.&amp;nbsp; If you have to use the text, and that is a real pain, then a simpler suggestion is to run it in SAS or DB then extract metadata, that takes all the parsing away.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 15:53:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453623#M114651</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-12T15:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453700#M114675</link>
      <description>&lt;P&gt;Hello Jerry&lt;/P&gt;
&lt;P&gt;The following code splits the create statement and extracts Column names and types. The Table name is extracted as the third word using SCAN, and the Column definitions are extracted using a regular expression, that looks for&amp;nbsp; a word in quotes followed by one space followed by an unquoted word optionally followed by a block containing only digits, parantheses or comma.&amp;nbsp;The expression also finds the keys, and they are omitted because the&amp;nbsp;&amp;nbsp;words PRIMARY and FOREIGN are not valid Column types.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
	line = 'CREATE TABLE "TEMP" ("TEMPID" NUMBER(7,0), "TESTID" NUMBER(6,0), "TYPE" VARCHAR2(5), "STARTDT" DATE, "SOURCE" VARCHAR2(5), "TITLE" VARCHAR2(6), CONSTRAINT "TEMP_PK" PRIMARY KEY ("TEMPID") ENABLE,  CONSTRAINT "TEMP_FK" FOREIGN KEY ("KEYID") REFERENCES "KEY" ("ID") ENABLE) ;';
run;

data want (keep=TABNm COLNm COLType); set have;
	length w $200 TABNm COLNm COLType $60;
	TABNm = dequote(scan(line,3,' '));

	xpr = prxparse('/"\w+"\s{1}\w+[\d,\(\)]*/');
	start = 1;
	stop = length(trim(line));
	call prxnext(xpr, start, stop, line, pos, len);
	do while (pos &amp;gt; 0);
		w = substr(line, pos, len-1);
		COLNm = dequote(scan(w,1,' '));
		COLType = dequote(scan(w,2,' '));
		if substr(COLType,1,4) not in ('PRIM','FORE') then output;
		call prxnext(xpr, start, stop, line, pos, len);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TEMP       TEMPID       NUMBER(7,0)
TEMP       TESTID       NUMBER(6,0)
TEMP       TYPE         VARCHAR2(5)
TEMP       STARTDT      DATE
TEMP       SOURCE       VARCHAR2(5)
TEMP       TITLE        VARCHAR2(6)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/453700#M114675</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2018-04-12T18:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Break up string using count function and how to loop through it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/455191#M115114</link>
      <description>Thank you everyone.  Erik this helped a lot.</description>
      <pubDate>Wed, 18 Apr 2018 14:07:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Break-up-string-using-count-function-and-how-to-loop-through-it/m-p/455191#M115114</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2018-04-18T14:07:38Z</dc:date>
    </item>
  </channel>
</rss>

