<?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: create new data set by looping over strings and compile the result into one data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/894997#M353546</link>
    <description>Hi..&lt;BR /&gt;&lt;BR /&gt;Actually, I have another use case but to simplify capture my problem, I use this case.</description>
    <pubDate>Wed, 20 Sep 2023 08:04:44 GMT</pubDate>
    <dc:creator>Moonlight_26</dc:creator>
    <dc:date>2023-09-20T08:04:44Z</dc:date>
    <item>
      <title>create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892705#M352602</link>
      <description>&lt;P&gt;Hi All..&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new in SAS and currently need a solution for my problem .. Here, I will explain about my issue with my sample data and process, kindly need your help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have list of name :&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name_without_space&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;DWIEYUDOPRIHANTO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;STANISURYANTO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SHERLYRITANANSY&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;HERERAFENAROSSA&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to make a specific table for each name and then finally compile them in to one data set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I wrote a macro like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;OPTIONS MPRINT;&lt;BR /&gt;proc sql ;&lt;BR /&gt;select distinct name_without_space into : name_list separated by ' ' from MyData&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;%macro dummy;&lt;/P&gt;&lt;P&gt;%do i=0 %to %sysfunc(countw(&amp;amp;name_list));&lt;/P&gt;&lt;P&gt;%let next_name = %scan(&amp;amp;name_list, &amp;amp;i);&lt;BR /&gt;%let value = %str(&amp;amp;next_name);&lt;BR /&gt;%let code = %substr(&amp;amp;value,1,5);&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table cek_data_&amp;amp;code as&lt;BR /&gt;select *&lt;BR /&gt;from MyData&lt;BR /&gt;where name_without_space = "&amp;amp;value" ;&lt;BR /&gt;%end;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table compile_all as&lt;/P&gt;&lt;P&gt;%do i=0 %to %sysfunc(countw(&amp;amp;name_list));&lt;BR /&gt;%let next_name = %scan(&amp;amp;name_list, &amp;amp;i);&lt;BR /&gt;%let value = %str(&amp;amp;next_name);&lt;BR /&gt;%let code = %substr(&amp;amp;value,1,5);&lt;/P&gt;&lt;P&gt;%if i = %sysfunc(countw(&amp;amp;name_list)) %then %do;&lt;BR /&gt;select *&lt;BR /&gt;from cek_data_&amp;amp;code&lt;BR /&gt;%end;&lt;BR /&gt;%else %do; /*%until (i = %sysfunc(countw(&amp;amp;name_list)));*/&lt;BR /&gt;select *&lt;BR /&gt;from cek_data_&amp;amp;code&lt;BR /&gt;outer union corr&lt;/P&gt;&lt;P&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;BR /&gt;%dummy;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works until make a specific table for each list name, but then I got error when try to create table&amp;nbsp;compile_all .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The errors :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: (, SELECT.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Seems the error caused by the last part of query still get the "outer union corr" like this :&amp;nbsp;select * from cek_data_HERER outer union corr ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly need help, so I can compile all data sets into one datasets. Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2023 11:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892705#M352602</guid>
      <dc:creator>Moonlight_26</dc:creator>
      <dc:date>2023-09-05T11:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892723#M352613</link>
      <description>&lt;P&gt;To better understand what your macro does, add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mlogic mprint symbolgen;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to your code.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2023 12:34:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892723#M352613</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-09-05T12:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892733#M352617</link>
      <description>&lt;P&gt;So pull data out of CEK_DATA_&amp;amp;code into separate data sets, based on the value in &amp;amp;next_name, and then combine all these data sets into one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No macros needed here. Do it all without macros. Do it all with one data pull.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
    select distinct cats('cek_data_',substr(name_without_space,1,5)) into :name_list separated by ' ' from MyData;
quit;

data compile_all;
     set &amp;amp;name_list;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2023 14:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892733#M352617</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-05T14:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892738#M352619</link>
      <description>&lt;P&gt;Can you generate the SQL query you are trying to use the macro to create by hand and get it to work?&lt;/P&gt;
&lt;P&gt;Can you share such a query and explain what parts of it need to change?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand why you would extract in pieces and then put the pieces back together.&lt;/P&gt;
&lt;P&gt;Why not just extract all at once?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set 
  cek_data_DWIEY
  cek_data_STANI
  cek_data_SHERL
  cek_data_HERER
  ;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Sep 2023 13:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892738#M352619</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-05T13:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892834#M352664</link>
      <description>&lt;P&gt;Thanks, Done that before, what I found for my problem is related to this part of macro :&lt;BR /&gt;&lt;BR /&gt;%else %do;&lt;BR /&gt;select *&lt;BR /&gt;from cek_data_&amp;amp;code&lt;BR /&gt;outer union corr&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;it seems that even for the last data set , still it call OUTER UNION CORR caused by the loop condition , so the ended query like below :&lt;BR /&gt;&lt;BR /&gt;select * from cek_data_SHERL outer union corr&lt;BR /&gt;select * from cek_data_HERER outer union corr ;&lt;BR /&gt;&lt;BR /&gt;It should be no "outer union corr" for the last dataset . Do yo have any idea for this ?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2023 03:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892834#M352664</guid>
      <dc:creator>Moonlight_26</dc:creator>
      <dc:date>2023-09-06T03:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892835#M352665</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/450997"&gt;@Moonlight_26&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, Done that before, what I found for my problem is related to this part of macro :&lt;BR /&gt;&lt;BR /&gt;%else %do;&lt;BR /&gt;select *&lt;BR /&gt;from cek_data_&amp;amp;code&lt;BR /&gt;outer union corr&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;it seems that even for the last data set , still it call OUTER UNION CORR caused by the loop condition , so the ended query like below :&lt;BR /&gt;&lt;BR /&gt;select * from cek_data_SHERL outer union corr&lt;BR /&gt;select * from cek_data_HERER outer union corr ;&lt;BR /&gt;&lt;BR /&gt;It should be no "outer union corr" for the last dataset . Do yo have any idea for this ?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't generate the extra one.&lt;/P&gt;
&lt;P&gt;You could add another %IF statement to determine whether you need that string generated or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I find it much easier to just use a macro variable for the separator characters.&amp;nbsp; Then I start with it empty and then set it after the first time the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the idea:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(string);
%local i sep;
%let sep=;
%do i=1 %to %sysfunc(countw(&amp;amp;string,|));
 &amp;amp;sep. %scan(&amp;amp;string,&amp;amp;i,|)
 %let sep=outer union corr;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;1273  %macro test(string);
1274  %Local i sep;
1275  %let sep=;
1276  %do i=1 %to %sysfunc(countw(&amp;amp;string,|));
1277   &amp;amp;sep. %scan(&amp;amp;string,&amp;amp;i,|)
1278   %let sep=outer union corr;
1279  %end;
1280  %mend;
1281  %put "%test(abc|def|xyz)";
" abc  outer union corr def  outer union corr xyz"
&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Sep 2023 03:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892835#M352665</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-06T03:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892838#M352668</link>
      <description>&lt;P&gt;I fail to understand why you would first split your single table into multiple tables only to combine them again into a single table. This is most of the time a sub-optimal approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because I'm apparently missing something below just some "food for thought".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Create some sample data&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyData;
  infile datalines truncover;
  input name_without_space $50.;
  datalines;
cek_data_DWIEY
cek_data_STANI
cek_data_STANI
cek_data_STANI
cek_data_SHERL
cek_data_HERER
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;2. Create list of distinct values for use with an IN operator.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let selection=;
proc sql noprint;
  select distinct cats("'",name_without_space,"'") into :selection separated by ','
  from MyData
  ;
quit;
%put %nrbquote(&amp;amp;selection);

data want;
  set mydata(where=(name_without_space in (&amp;amp;selection)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or here for your problem to create a select * &amp;lt;table&amp;gt; union all corr&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let combine=;
proc sql noprint;
  select distinct catx(' ','select * from',name_without_space) into :combine separated by ' union all corr '
  from MyData
  ;
quit;
%put %nrbquote(&amp;amp;combine);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From SAS log:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;select * from cek_data_DWIEY union all corr select * from cek_data_HERER union all corr select * from cek_data_SHERL union all corr select * from cek_data_STANI&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And last but not least: SAS macro language complicates code and should only be used if you need something dynamic that you can't achieve with "normal" SAS language. It's a rather common beginners mistake to use macro language too quickly. If you explain us a bit more in detail what you have and what you need then it's likely someone can suggest an approach that doesn't require SAS macro code at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2023 05:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/892838#M352668</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-09-06T05:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/894996#M353545</link>
      <description>Thank You,&lt;BR /&gt;My problem solved.</description>
      <pubDate>Wed, 20 Sep 2023 08:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/894996#M353545</guid>
      <dc:creator>Moonlight_26</dc:creator>
      <dc:date>2023-09-20T08:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/894997#M353546</link>
      <description>Hi..&lt;BR /&gt;&lt;BR /&gt;Actually, I have another use case but to simplify capture my problem, I use this case.</description>
      <pubDate>Wed, 20 Sep 2023 08:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/894997#M353546</guid>
      <dc:creator>Moonlight_26</dc:creator>
      <dc:date>2023-09-20T08:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: create new data set by looping over strings and compile the result into one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/895002#M353550</link>
      <description>&lt;P&gt;You already have the answer but just for fun &lt;A href="https://github.com/yabwon/SAS_PACKAGES" target="_blank" rel="noopener"&gt;SAS Packages&lt;/A&gt; approach with &lt;A href="https://github.com/SASPAC/macroarray" target="_blank" rel="noopener"&gt;MacroArray&lt;/A&gt; package:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Name_without_space $32.;
cards;
DWIEYUDOPRIHANTO
STANISURYANTO
SHERLYRITANANSY
HERERAFENAROSSA
;
run;


filename packages "/path/to/my/packages";
%include packages(SPFinit.sas);
%loadPackage(macroArray)


/*
%helpPackage(macroarray,'%array()')
*/

data have2;
  set have;
  length short_names $ 14;
  short_names = 'cek_data_' !! Name_without_space;
run;

%array(ds = have2, vars = short_names|, macarray=Y)


/* %put %do_over(short_names) ; */
     
data want;
  set %do_over(short_names) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:42:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-data-set-by-looping-over-strings-and-compile-the/m-p/895002#M353550</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-20T08:42:11Z</dc:date>
    </item>
  </channel>
</rss>

