<?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 program help in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539553#M7079</link>
    <description>&lt;P&gt;Why are you splitting the data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want to select the records where the variable UPC7 is equal to the variable AA?&amp;nbsp; Or has the value "AA"?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sing(solar);
data test_&amp;amp;solar;
  set perm.jupiter;
  if UPC7 = "&amp;amp;solar";
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make the splitting dynamic get the list of possible values and use that to generate the macro calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=perm.jupiter(keep=upc7) out=list nodupkey;
  by upc7;
run;
data _null_;
  set list;
  call execute('%nrstr(%sing)('||upc7||')');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Mar 2019 07:40:18 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-03-01T07:40:18Z</dc:date>
    <item>
      <title>macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539533#M7077</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using Enterprise Guide 7.1 and SAS 9.4.&amp;nbsp; I have a dataset with 3 variables one of them being UPC7 which signifies different product types.&amp;nbsp; I want to create a dataset named for each&amp;nbsp;the product type. The types may change from month to month, so I want it based on what is in the main dataset.&amp;nbsp; So each dataset will only have several observations of that product type.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;amp;MACRO sing (solar);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;data test_&amp;amp;solar;&lt;/P&gt;
&lt;P&gt;set perm.jupiter;&lt;/P&gt;
&lt;P&gt;if UPC7&amp;nbsp;= &amp;amp;solar;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;%MEND; %sing (upc7name);&amp;nbsp;&lt;/P&gt;
&lt;P&gt;instead of listing each product code&lt;/P&gt;
&lt;P&gt;%sing(AA);&amp;nbsp; %sing(AB); %sing(CG);&amp;nbsp; %sing(BA); etc&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 01:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539533#M7077</guid>
      <dc:creator>pangea17</dc:creator>
      <dc:date>2019-03-01T01:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539537#M7078</link>
      <description>&lt;P&gt;The best way to do this is with hashing.&amp;nbsp; Might as well listen to the experts:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Splitting-a-SAS-data-set-based-on-the-value-of-a-variable/ta-p/489104" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Splitting-a-SAS-data-set-based-on-the-value-of-a-variable/ta-p/489104&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another alternative is to leave the data as is, and process using a BY statement.&amp;nbsp; If absolutely necessary, you can split the data using macro language, but it isn't necessary given the hashing solution.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 03:29:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539537#M7078</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-01T03:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539553#M7079</link>
      <description>&lt;P&gt;Why are you splitting the data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want to select the records where the variable UPC7 is equal to the variable AA?&amp;nbsp; Or has the value "AA"?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sing(solar);
data test_&amp;amp;solar;
  set perm.jupiter;
  if UPC7 = "&amp;amp;solar";
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make the splitting dynamic get the list of possible values and use that to generate the macro calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=perm.jupiter(keep=upc7) out=list nodupkey;
  by upc7;
run;
data _null_;
  set list;
  call execute('%nrstr(%sing)('||upc7||')');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 07:40:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539553#M7079</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-01T07:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539568#M7082</link>
      <description>&lt;P&gt;If you need to perform analysis on single subsets of the data, create an index on the variable in question to speed up where conditions.&lt;/P&gt;
&lt;P&gt;If you need analysis &lt;EM&gt;grouped&lt;/EM&gt; on that variable (across the whole or most of the dataset), use &lt;FONT face="courier new,courier"&gt;by&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;class&lt;/FONT&gt; in the respective procedures. Splitting is not needed in most cases, needs extra steps (and therefore degrades performance), and increases storage consumption.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 09:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539568#M7082</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-01T09:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539618#M7088</link>
      <description>&lt;P&gt;OK, with a few warnings ... it may not be right to split up the data at all, hashing is a better way, and macro language contains pitfalls ... here is a long-winded explanation that eventually illustrates a macro language solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, look at the objective you are setting up.&amp;nbsp; You are trying to set up 100 or so DATA steps along these lines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_AA;
   set perm.jupiter;
   if UPC7 = "AA";
run;

data test_AB;
   set perm.jupiter;
   if UPC7 = "AB";
run;

data test_CG;
   set perm.jupiter;
   if UPC7 = "CG";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That plan illustrates one of the pitfalls of macro language:&amp;nbsp; it's easy to abuse.&amp;nbsp; Carried out, that plan would have to read in all the data from perm.jupiter 100 times.&amp;nbsp; It would be much faster to read in the data once, along these lines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_AA test_AB test_CG;
   set perm.jupiter;
   if UPC7 = "AA" then output test_AA;
   else if UPC7 = "AB" then output test_AB;
   else if UPC7 = "CG" then output test_CG;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since your data sets are small, it might not make a difference to you.&amp;nbsp; Perhaps the flawed version with 100 DATA steps takes a minute to run, while the one-data-step version takes a couple of seconds to run.&amp;nbsp; However, now is the time to practice good habits.&amp;nbsp; If your data sets become larger, perhaps you are taking an hour to run instead of a minute or two.&amp;nbsp; So practice when you have small data sets and can afford the time to experiment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On a side note, switching your original DATA steps from an IF statement to a WHERE statement would probably run faster as well.&amp;nbsp; But that's another topic for another day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So how do you get macro language to generate one DATA step?&amp;nbsp; Here's an approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro language can get a list of all UPC7 codes, and store that list in a macro variable.&amp;nbsp; It can process the list to generate the required one-data-step program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro many_upc7;

%local k next_code;

proc sql;
   select distinct(upc7) into : upc7_list  separated by " "  from perm.jupiter;
quit;

data 
   %do k=1 %to %sysfunc(countw(&amp;amp;upc7_list));
      %let next_code = %scan(&amp;amp;upc7_list, &amp;amp;k);
      test_&amp;amp;next_code
   %end;
   ;
   set perm.jupiter;
   %do k=1 %to %sysfunc(countw(&amp;amp;upc7_list));
      %let next_code = %scan(&amp;amp;upc7_list, &amp;amp;k);
      %if &amp;amp;k &amp;gt; 1 %then else;
      if upc7 = "&amp;amp;next_code" then output test_&amp;amp;next_code;
   %end;
run;

%mend many_upc7;

%many_upc7
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Admittedly, it uses some sophisticated coding.&amp;nbsp; (It is untested, since you have access to the data.)&amp;nbsp; On the other hand, it's realistic in terms of the tools that would be used in real life.&amp;nbsp; So if you have questions about it, fire away.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note, SQL counts how many observations it returns.&amp;nbsp; It should be possible to use the automatic variable &amp;amp;SQLOBS instead of counting with %sysfunc(countw(&amp;amp;upc7_list))&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 14:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539618#M7088</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-01T14:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539739#M7113</link>
      <description>&lt;P&gt;I am splitting the data to run some proc arima code. I could use a by statement, but that would mean that each&amp;nbsp;by group&amp;nbsp;has to use the same model, which might not be the way to go depending on the data.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 18:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539739#M7113</guid>
      <dc:creator>pangea17</dc:creator>
      <dc:date>2019-03-01T18:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: macro program help</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539740#M7114</link>
      <description>In that case just use a WHERE statement in your model.&lt;BR /&gt;</description>
      <pubDate>Fri, 01 Mar 2019 18:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-program-help/m-p/539740#M7114</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-01T18:46:47Z</dc:date>
    </item>
  </channel>
</rss>

