<?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: How to generate dataset with macro function and looping? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950015#M371548</link>
    <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;'s code showing there is no need for a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One minor suggestion that could greatly improve efficiency if the dataset is large.&amp;nbsp; Generate ALL as a dataset VIEW rather than as a dataset FILE.&amp;nbsp; This will allow the two passthroughs of HAVE to be synchronized rather than one at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{added comment).&amp;nbsp; As a result there will be no need to write the intermediate dataset ALL to disk, just to immediately reread it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ALL / view=ALL;
  ...
data WANT;
  ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2024 15:14:05 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-11-06T15:14:05Z</dc:date>
    <item>
      <title>How to generate dataset with macro function and looping?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/949999#M371543</link>
      <description>&lt;P&gt;Hi community! I am not familiar with SAS macro function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got a dataset like the one below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASnewbie0_0-1730864981133.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101981iC1BBCA394C105A9B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASnewbie0_0-1730864981133.png" alt="SASnewbie0_0-1730864981133.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I would like to add row (in red).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASnewbie0_1-1730865068466.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101982i759520FD1DD3CAF1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASnewbie0_1-1730865068466.png" alt="SASnewbie0_1-1730865068466.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have an idea creating records with macro function and looping.&lt;/P&gt;&lt;P&gt;I have created an array for month and table with shop that need generate records.&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Array:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;_N_&amp;nbsp; &amp;nbsp; &amp;nbsp; month&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2023-01&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2023-02&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2023-03&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Table:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;store&amp;nbsp; &amp;nbsp; &amp;nbsp; cnt&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on getting a dataset&amp;nbsp; like the one below?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASnewbie0_2-1730865871541.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101983iE610BB8FF3B07485/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASnewbie0_2-1730865871541.png" alt="SASnewbie0_2-1730865871541.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 04:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/949999#M371543</guid>
      <dc:creator>SASnewbie0</dc:creator>
      <dc:date>2024-11-06T04:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate dataset with macro function and looping?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950000#M371544</link>
      <description>&lt;P&gt;One possibility may be to create a table containing all the store IDs and months that you want, and then merge with the original:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
  set have(keep=store);
  by  store;
  if first.store;
  do month='2023-01','2023-02','2023-03';
    output;
    end;
run;

data want;
  merge have(in=ok) all;
  by store month;
  if not ok then
    sales=-999;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I do not think there is any reason to use macros for this.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 04:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950000#M371544</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-11-06T04:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate dataset with macro function and looping?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950001#M371545</link>
      <description>&lt;P&gt;Below approach is performance wise not optimal but it's easy to code and is dynamic.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input store $ _dt $ sales;
  format date yymmd7.;
  date=input(catx('-',_dt,'01'),yymmdd10.);
  drop _dt;
datalines;
A 2023-01 100
A 2023-02 50
A 2023-03 120
B 2023-02 110
B 2023-03 90
C 2023-03 80
;
run;

proc sql;
/*  create table want as*/
  select 
    t1.store
    ,t2.date
    ,coalesce(t3.sales,-999) as sales

  from (select distinct store from have) t1 

  full join (select distinct date from have) t2
  on 1

  left join have t3
  on t3.store=t1.store and t3.date=t2.date
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you just need to ensure that you've got always the same values for categorical variables when it comes to reporting then eventually do not modify your source table but look into the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n1tbs0k1djpsbtn1xi0arrt30aby.htm" target="_self"&gt;classdata&lt;/A&gt; option available with certain procedures like Proc Means and Proc Tabulate.&lt;/P&gt;
&lt;P&gt;And for reporting if you want to print missing values as -999 then look into SAS option missing=...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 05:51:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950001#M371545</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-11-06T05:51:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate dataset with macro function and looping?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950015#M371548</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;'s code showing there is no need for a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One minor suggestion that could greatly improve efficiency if the dataset is large.&amp;nbsp; Generate ALL as a dataset VIEW rather than as a dataset FILE.&amp;nbsp; This will allow the two passthroughs of HAVE to be synchronized rather than one at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{added comment).&amp;nbsp; As a result there will be no need to write the intermediate dataset ALL to disk, just to immediately reread it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ALL / view=ALL;
  ...
data WANT;
  ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 15:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-generate-dataset-with-macro-function-and-looping/m-p/950015#M371548</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-11-06T15:14:05Z</dc:date>
    </item>
  </channel>
</rss>

