<?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 datasets with name of each make in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873375#M345082</link>
    <description>&lt;P&gt;Or perhaps better is use the same rule in the WHERE clause that subsets the data as that avoids dataset name collisions.&lt;/P&gt;
&lt;P&gt;So all of the records for makes like 'X-Y' and 'X/Y' will end up in the same dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;&amp;amp;make&amp;amp;i;
  set sashelp.cars;
  where compress(make,,'kn')="&amp;amp;&amp;amp;make&amp;amp;i"; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also need to use UPCASE() function in both places to avoid name collisions causes by name like 'FORD' and 'Ford'.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 May 2023 14:11:21 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-02T14:11:21Z</dc:date>
    <item>
      <title>create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873297#M345038</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select  count(make) as total 
into:n
from sashelp.cars;
select make
into:make1-:make99%left(&amp;amp;n)
from sashelp.cars;
quit;


%macro cars;
%do i=1 %to &amp;amp;n;
data &amp;amp;&amp;amp;make&amp;amp;i;
set sashelp.cars;
if make="&amp;amp;&amp;amp;make&amp;amp;i";
run;
%end;
%mend;
%cars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;error below&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Anandkvn_0-1682999193295.png" style="width: 770px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83485i5E993DF40E81D6F0/image-dimensions/770x466?v=v2" width="770" height="466" role="button" title="Anandkvn_0-1682999193295.png" alt="Anandkvn_0-1682999193295.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 03:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873297#M345038</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-05-02T03:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873300#M345041</link>
      <description>&lt;P&gt;You cannot use a string like&amp;nbsp;Mercedes-Benz as the name of a dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Come up with another way to name your datasets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps just number them and put the value of MAKE into the LABEL of the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=sashelp.cars;
 tables make / out=makes noprint;
run;

filename code temp;
data _null_;
  set makes ;
  make_num +1;
  file code ;
  put 'data make' make_num '(label=' make :$quote. ');'
    / '  set sashelp.cars;'
    / '  where ' make = $quote. ';'
    / 'run;'
  ;
run;

%include code / source2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;379 +data make23 (label="Mazda" );
380 +  set sashelp.cars;
381 +  where Make="Mazda" ;
382 +run;

NOTE: There were 11 observations read from the data set SASHELP.CARS.
      WHERE Make='Mazda';
NOTE: The data set WORK.MAKE23 has 11 observations and 15 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


383 +data make24 (label="Mercedes-Benz" );
384 +  set sashelp.cars;
385 +  where Make="Mercedes-Benz" ;
386 +run;

NOTE: There were 26 observations read from the data set SASHELP.CARS.
      WHERE Make='Mercedes-Benz';
NOTE: The data set WORK.MAKE24 has 26 observations and 15 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 May 2023 04:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873300#M345041</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-02T04:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873301#M345042</link>
      <description>&lt;P&gt;Hi&amp;nbsp; Tom,&lt;/P&gt;
&lt;P&gt;i want output same like below code using cars dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select count(name) as total_obs&lt;BR /&gt;into : n&lt;BR /&gt;from sashelp.class;&lt;BR /&gt;/* select name */&lt;BR /&gt;/* into: name1 -:name%left(&amp;amp;n) */&lt;BR /&gt;/* from sashelp.class; */&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%macro make_ds;&lt;BR /&gt;%do i=1 %to &amp;amp;n ;&lt;BR /&gt;data &amp;amp;&amp;amp;name&amp;amp;i ;&lt;BR /&gt;set sashelp.class;&lt;BR /&gt;if name="&amp;amp;&amp;amp;name&amp;amp;i";&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend make_ds;&lt;BR /&gt;%make_ds;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 05:18:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873301#M345042</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-05-02T05:18:45Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873302#M345043</link>
      <description>&lt;P&gt;&amp;nbsp;Read &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s post again.&lt;/P&gt;
&lt;P&gt;You&amp;nbsp;&lt;STRONG&gt;cannot&lt;/STRONG&gt; (you know what "cannot" means?) use a string like "Merceces-Benz" as a dataset name. You must come up with a different naming scheme.&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 05:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873302#M345043</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-02T05:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873304#M345044</link>
      <description>&lt;P&gt;As others already stated you can't use a dash in a SAS table name even if you treat it as a literal. That's documented &lt;A href="https://go.documentation.sas.com/doc/en/lrcon/9.4/p18cdcs4v5wd2dn1q0x296d3qek6.htm" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1683005963734.png" style="width: 704px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83486iC43477E8B231D55B/image-dimensions/704x32?v=v2" width="704" height="32" role="button" title="Patrick_0-1683005963734.png" alt="Patrick_0-1683005963734.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Not that it is advisable in real life situations to split a SAS table into many SAS tables by some category but assuming this is just a learning exercise:&lt;BR /&gt;You could just remove any character that's not suitable for use as a SAS table name. Code like below should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct make) 
    into :n trimmed
  from sashelp.cars;
  select distinct compress(make,,'kn') 
    into:make1-:make&amp;amp;n
  from sashelp.cars;
quit;

%macro cars();
  %do i=1 %to &amp;amp;n;
    data &amp;amp;&amp;amp;make&amp;amp;i;
      set sashelp.cars;
      if make="&amp;amp;&amp;amp;make&amp;amp;i";
    run;
  %end;
%mend;

%cars();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 05:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873304#M345044</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-05-02T05:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873312#M345048</link>
      <description>&lt;P&gt;Hi Patrick ,&lt;/P&gt;
&lt;P&gt;Thanks for your solution but Merdesbenz dataset no observations&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 07:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873312#M345048</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-05-02T07:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873314#M345049</link>
      <description>&lt;P&gt;Since the data set name variable is changed you now need to add another variable that holds the actual value of the variable to do the selection.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sql noprint;
  select count(distinct make) 
    into :n trimmed
  from sashelp.cars;
  select distinct compress(make,,'kn') 
    into:make1-:make&amp;amp;n
  from sashelp.cars;
  select distinct make 
    into:makevalue1-:makevalue&amp;amp;n
  from sashelp.cars;
quit;

%macro cars();
  %do i=1 %to &amp;amp;n;
    data &amp;amp;&amp;amp;make&amp;amp;i;/*&amp;lt; data set name value*/
      set sashelp.cars;
      if make="&amp;amp;&amp;amp;makevalue&amp;amp;i"; /*&amp;lt;variable value*/
    run;
  %end;
%mend;&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 02 May 2023 08:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873314#M345049</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-02T08:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873369#M345078</link>
      <description>&lt;P&gt;I do not like that method of generating zillions of macro variables instead of keeping the data in a dataset ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you are going to do it then do it right.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;There is no need to run the query twice, proc sql can count.&lt;/LI&gt;
&lt;LI&gt;Make a separate set of macro variables to hold the dataset names so you can add rules to insure the dataset names are valid.&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_ds
(indsname  /* Input dataset name */
,splitvar  /* Variable to split on */
);
proc sql noprint;
select distinct
    quote(trim(&amp;amp;splitvar),"'") 
  , compress(&amp;amp;splitvar,,'kn')
  into :name1-
     , :dsn1-
  from &amp;amp;indsname
;
%let n=&amp;amp;sqlobs;
quit;
%do i=1 %to &amp;amp;n;
data &amp;amp;&amp;amp;dsn&amp;amp;i;
  set &amp;amp;indsname;
  if &amp;amp;splitvar=&amp;amp;&amp;amp;name&amp;amp;i;
run;
%end;
%mend split_ds;

options mprint ;
%split_ds(sashelp.cars,make)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 14:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873369#M345078</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-02T14:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873375#M345082</link>
      <description>&lt;P&gt;Or perhaps better is use the same rule in the WHERE clause that subsets the data as that avoids dataset name collisions.&lt;/P&gt;
&lt;P&gt;So all of the records for makes like 'X-Y' and 'X/Y' will end up in the same dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;&amp;amp;make&amp;amp;i;
  set sashelp.cars;
  where compress(make,,'kn')="&amp;amp;&amp;amp;make&amp;amp;i"; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also need to use UPCASE() function in both places to avoid name collisions causes by name like 'FORD' and 'Ford'.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 14:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873375#M345082</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-02T14:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873378#M345083</link>
      <description>&lt;P&gt;One more solution for lazy programmers who want SAS to rename their datasets names into correct form:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ds=sashelp.cars;
%let var=Make;

data names_list;
  set &amp;amp;ds.(keep=&amp;amp;var.);
run;

proc sort data=names_list nodupkey;
  by &amp;amp;var.;
run;

proc transpose data=names_list out=tmp;
  var &amp;amp;var.;
  Id &amp;amp;var.;
run;

proc transpose data=tmp(drop=_name_) out=tmp(rename=(col1=&amp;amp;var.));
  var _all_;
run;

proc print data=tmp;
run;

data _null_;
  set tmp;
  call execute(cat('data work.',_name_,';'));
  call execute("set &amp;amp;ds.;");
  call execute(cat('where Make ="',&amp;amp;var.,'";'));
  call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 14:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873378#M345083</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-02T14:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: create datasets with name of each make</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873483#M345124</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Patrick ,&lt;/P&gt;
&lt;P&gt;Thanks for your solution but Merdesbenz dataset no observations&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;True. Just add the compress() function also to the if condition. Below will work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct make) 
    into :n trimmed
  from sashelp.cars;
  select distinct compress(make,,'kn') 
    into:make1-:make&amp;amp;n
  from sashelp.cars;
quit;

%macro cars();
  %do i=1 %to &amp;amp;n;
    data &amp;amp;&amp;amp;make&amp;amp;i;
      set sashelp.cars;
      if compress(make,,'kn')="&amp;amp;&amp;amp;make&amp;amp;i";
    run;
  %end;
%mend;

%cars();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a hash table would be an alternative and more efficient coding option as it reads the source data only once.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if _n_=1 then 
    do;
      dcl hash h1(multidata:'y', dataset:'sashelp.cars(obs=0)');
      h1.defineKey('make');
      h1.defineData(all:'y');
      h1.defineDone();
    end;
  set sashelp.cars;
  by make;
  _rc=h1.add();
  if last.make then
    do;
      _rc=h1.output(dataset:cats('work.',compress(make,,'kn')));
      _rc=h1.clear();
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 20:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-datasets-with-name-of-each-make/m-p/873483#M345124</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-05-02T20:48:02Z</dc:date>
    </item>
  </channel>
</rss>

