<?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 Use a variable dynamically in a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789329#M252566</link>
    <description>&lt;P&gt;hello, new to sas eg. mostly know tsql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need assign the result-set of a proc sql query into a table variable, and then i need to call that variable within a data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the proc sql query:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc  sql;
  create table work.finalcode as
  select
	cats(substr(id,1,7),row_number,substr(id,9,14),row_number,substr(id,24,length(id) ) ) as code
from chhccs_result;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the result of the above query are 4 rows:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;address1= scan(column,1,',')&lt;/LI&gt;&lt;LI&gt;address2= scan(column,2,',')&lt;/LI&gt;&lt;LI&gt;address3= scan(column,3,',')&lt;/LI&gt;&lt;LI&gt;address4= scan(column,4,',')&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;the above 4 rows need to be assigned to a variable if possible, then referenced in a subsequent data step, as part of the data step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.test;
set work.test;
VARIABLE THAT LISTS THE 4 ROWS HERE
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;so that when the data step runs, its runs as if its written like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.test;
set work.test;
address1= scan(column,1,',');
address2= scan(column,2,',');
address3= scan(column,3,',');
address4= scan(column,4,',');
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;thanks in advance, spent all day trying to figure this out&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jan 2022 19:26:41 GMT</pubDate>
    <dc:creator>ryanb0001</dc:creator>
    <dc:date>2022-01-10T19:26:41Z</dc:date>
    <item>
      <title>Use a variable dynamically in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789329#M252566</link>
      <description>&lt;P&gt;hello, new to sas eg. mostly know tsql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need assign the result-set of a proc sql query into a table variable, and then i need to call that variable within a data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the proc sql query:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc  sql;
  create table work.finalcode as
  select
	cats(substr(id,1,7),row_number,substr(id,9,14),row_number,substr(id,24,length(id) ) ) as code
from chhccs_result;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the result of the above query are 4 rows:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;address1= scan(column,1,',')&lt;/LI&gt;&lt;LI&gt;address2= scan(column,2,',')&lt;/LI&gt;&lt;LI&gt;address3= scan(column,3,',')&lt;/LI&gt;&lt;LI&gt;address4= scan(column,4,',')&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;the above 4 rows need to be assigned to a variable if possible, then referenced in a subsequent data step, as part of the data step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.test;
set work.test;
VARIABLE THAT LISTS THE 4 ROWS HERE
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;so that when the data step runs, its runs as if its written like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.test;
set work.test;
address1= scan(column,1,',');
address2= scan(column,2,',');
address3= scan(column,3,',');
address4= scan(column,4,',');
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;thanks in advance, spent all day trying to figure this out&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 19:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789329#M252566</guid>
      <dc:creator>ryanb0001</dc:creator>
      <dc:date>2022-01-10T19:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Use a variable dynamically in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789334#M252570</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how about using SAS macro?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(n);
  %do n= 1 %to &amp;amp;n.;
    address&amp;amp;n.= scan(column, &amp;amp;n., ',');
  %end;
%mend loop;

options mprint;
data work.test;
  set work.test;
  %loop(4)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the content of the:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;chhccs_result&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 19:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789334#M252570</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-01-10T19:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Use a variable dynamically in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789335#M252571</link>
      <description>&lt;P&gt;I would start with something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;proc  sql;

  select
	cats(substr(id,1,7),row_number,substr(id,9,14),row_number,substr(id,24,length(id) ) ) into: alist separated by "|"
from chhccs_result;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;to create a delimited macro variable. &lt;/P&gt;
&lt;P&gt;Then following your example (but this adds the exact same 4 values to every record in the Work.Test data set. Is that actually what you want???)&lt;/P&gt;
&lt;PRE&gt;data work.NEWDATASET;
set work.test;
array address(4) $ 200;&lt;BR /&gt;do i=1 to 4;
   address[i]= scan("&amp;amp;alist",i,'|');
end;
run; &lt;/PRE&gt;
&lt;P&gt;If you do not &lt;STRONG&gt;know&lt;/STRONG&gt; the number of records in the chhccs_result data set then IMMEDIATELY after the Proc Sql shown run&lt;/P&gt;
&lt;PRE&gt;%let rcount =&amp;amp;sqlobs;&lt;/PRE&gt;
&lt;P&gt;This will capture the number of records pulled and then use &amp;amp;rcount instead of 4 in in the data set.&lt;/P&gt;
&lt;P&gt;Are you sure that want to create 4 (or more variables)? That can be hard to work with. Why do you need them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use of the same data set name in the output as in the Set statement can create problems with logic errors or unexpected behaviors because you completely rewrite the data set. Which means that with a different&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 19:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-a-variable-dynamically-in-a-data-step/m-p/789335#M252571</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-10T19:46:38Z</dc:date>
    </item>
  </channel>
</rss>

