<?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: Macro variable sequence in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871471#M344271</link>
    <description>&lt;P&gt;What would the rule be, based on string length? Would it be find the longest string in your dataset and divide by 30? Lets say your maximum string length was 1024 (based on the position of the rightmost character in the string). 1024 / 30 is 34.13. That means you will need 35 30-character variables to store this. You can easily calculate it like this and put the result into macro variable you can use in following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select ceil(max(length(string))/30)
  into :VarNum
  from MyLib.MyTable;
quit;
%put VarNum = &amp;amp;VarNum;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 23 Apr 2023 22:33:16 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2023-04-23T22:33:16Z</dc:date>
    <item>
      <title>Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871366#M344209</link>
      <description>&lt;P&gt;I created a data step macro to split a long string into a fixed length but also maintain text readability by splitting at the space closest to the fixed length. For example, a comment has 113 characters. To split it into 4 pieces each with max of 30 characters. The macro works, but I don't know how to simplify &amp;amp;Var.1, &amp;amp;Var.2 etc. There should be a simple way to say like &lt;EM&gt;else &amp;amp;var.i=....&lt;/EM&gt; Below is the macro and the test.&lt;/P&gt;
&lt;PRE&gt;%macro split_text(txt=,var=);
	length __txt $2000;
	__txt=&amp;amp;txt;	
	m=30;
	len=length(__txt);
	n=floor(len/m);

	do i=0 to n;
		j=find(__txt,' ',-m);
		if i=0 then &amp;amp;var=substr(__txt,1,j-1);
		if i=1 then &amp;amp;var.1=substr(__txt,1,j-1);
		if i=2 then &amp;amp;var.2=substr(__txt,1,j-1);
		if i=3 then &amp;amp;var.3=substr(__txt,1,j-1);
		if i=4 then &amp;amp;var.4=substr(__txt,1,j-1);

		*reset;
		__txt=substr(__txt,j+1);
	end;
%mend;

data test;
	comment = 'When splitting a text string into several records, the text should be split between words to improve readability.';
	%split_text(txt=comment,var=COM);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Below is the result.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="COM.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83047i58564517DB69C048/image-size/medium?v=v2&amp;amp;px=400" role="button" title="COM.png" alt="COM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Apr 2023 02:02:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871366#M344209</guid>
      <dc:creator>jsbyxws</dc:creator>
      <dc:date>2023-04-23T02:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871371#M344212</link>
      <description>&lt;P&gt;Maybe what is missing is an array. Something like this might help (untested):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_text(txt=,var=);
	length __txt $2000;
	__txt=&amp;amp;txt;	
	m=30;
	len=length(__txt);
	n=floor(len/m);
    array vars (*) $ 30 &amp;amp;var.0 - &amp;amp;var.4;
	do i=0 to n;
		j=find(__txt,' ',-m);
        vars(i) = substr(__txt,1,j-1);
		*reset;
		__txt=substr(__txt,j+1);
	end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Apr 2023 02:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871371#M344212</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-04-23T02:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871404#M344226</link>
      <description>&lt;P&gt;Thanks a lot! It works!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another question: how to dynamically assign exact number of variables based on string length? Extra variables with blank values should be removed.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Apr 2023 14:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871404#M344226</guid>
      <dc:creator>jsbyxws</dc:creator>
      <dc:date>2023-04-23T14:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871471#M344271</link>
      <description>&lt;P&gt;What would the rule be, based on string length? Would it be find the longest string in your dataset and divide by 30? Lets say your maximum string length was 1024 (based on the position of the rightmost character in the string). 1024 / 30 is 34.13. That means you will need 35 30-character variables to store this. You can easily calculate it like this and put the result into macro variable you can use in following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select ceil(max(length(string))/30)
  into :VarNum
  from MyLib.MyTable;
quit;
%put VarNum = &amp;amp;VarNum;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Apr 2023 22:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871471#M344271</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-04-23T22:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871483#M344272</link>
      <description>&lt;P&gt;Sorry, I did not make myself clear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I mean in data step, assigning array with &amp;amp;var.1-&amp;amp;var.4 is based on current string length of 113. But if next string length is only 70, then only &amp;amp;var.1-&amp;amp;var.3 is needed. How to assign array with exact number of variables based on the string length?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2023 03:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871483#M344272</guid>
      <dc:creator>jsbyxws</dc:creator>
      <dc:date>2023-04-24T03:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871505#M344274</link>
      <description>&lt;P&gt;You solve this by splitting vertically instead of horizontally. You will have a dataset with three variables (a key for identification, a sequence number, and the text) with a variable number of observations per key. This is the most efficient way to store variable-length texts in smaller variables&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2023 06:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871505#M344274</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-24T06:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable sequence in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871611#M344342</link>
      <description>&lt;P&gt;Thanks Kurt!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I raised the 2nd question just want to be perfect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In reality I can handle it with one more step "if not blank, then output to dataset".&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2023 14:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-sequence-in-data-step/m-p/871611#M344342</guid>
      <dc:creator>jsbyxws</dc:creator>
      <dc:date>2023-04-24T14:48:20Z</dc:date>
    </item>
  </channel>
</rss>

