<?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 Creating variables within a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57540#M12417</link>
    <description>Am trying to create three new indicator variables k_200907, k_200908 and k_200909 into dataset B via macro below, but only created variable k_200909 in the end.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro naruto(var1=);&lt;BR /&gt;
&lt;BR /&gt;
data work.datasetB;&lt;BR /&gt;
   set work.datasetA;&lt;BR /&gt;
                 if date &amp;lt; &amp;amp;var1 then k_&amp;amp;var1 = 1;&lt;BR /&gt;
          else if date &amp;gt;= &amp;amp;var1 then k_&amp;amp;var1 = 0;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend naruto;&lt;BR /&gt;
&lt;BR /&gt;
%naruto(var1 = 200907)&lt;BR /&gt;
%naruto(var1 = 200908)&lt;BR /&gt;
%naruto(var1 = 200909)&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Please could anyone shed some light on why variable k_200907 and  k_200908 aren't stored in dataset B?  Many thanks.  Regards Silverflute</description>
    <pubDate>Thu, 29 Jul 2010 15:18:28 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-07-29T15:18:28Z</dc:date>
    <item>
      <title>Creating variables within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57540#M12417</link>
      <description>Am trying to create three new indicator variables k_200907, k_200908 and k_200909 into dataset B via macro below, but only created variable k_200909 in the end.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro naruto(var1=);&lt;BR /&gt;
&lt;BR /&gt;
data work.datasetB;&lt;BR /&gt;
   set work.datasetA;&lt;BR /&gt;
                 if date &amp;lt; &amp;amp;var1 then k_&amp;amp;var1 = 1;&lt;BR /&gt;
          else if date &amp;gt;= &amp;amp;var1 then k_&amp;amp;var1 = 0;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend naruto;&lt;BR /&gt;
&lt;BR /&gt;
%naruto(var1 = 200907)&lt;BR /&gt;
%naruto(var1 = 200908)&lt;BR /&gt;
%naruto(var1 = 200909)&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Please could anyone shed some light on why variable k_200907 and  k_200908 aren't stored in dataset B?  Many thanks.  Regards Silverflute</description>
      <pubDate>Thu, 29 Jul 2010 15:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57540#M12417</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-29T15:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57541#M12418</link>
      <description>Hi:&lt;BR /&gt;
  Simply put, the SAS Macro Facility is like a big, automatic typewriter. Every time you invoke your macro program (%NARUTO), the Macro word scanner and tokenizer is typing the WHOLE DATA step program over again. &lt;BR /&gt;
&lt;BR /&gt;
  So the first invocation creates the resolved code and when the resolved code runs, WORK.DATASETB is created. However, the SECOND invocation again types the DATA step code, with the new values and now THAT resolved code is compiling and executing and is writing OVER the first WORK.DATASETB. And THEN, the same thing happens again the THIRD time you invoke your macro program. So what you're left with is the 3rd version of WORK.DATASETB.&lt;BR /&gt;
 &lt;BR /&gt;
  I would recommend that you read up a bit on how SAS Macros work by searching for previous forum postings or using Google to search for SAS Macro tutorials from user groups. Here's a paper to get you started:&lt;BR /&gt;
 &lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
  If you want to create variable k_200907 and k_200908 and k_200909 in the same dataset, you will have to redesign how your macro program works and how it generates the DATA step program. Here's a hint, a macro program can generate WHOLE steps, such as whole procedure steps or whole DATA step programs or a macro program can generate parts of statements within a procedure or program or can generate multiple statements. Your macro program does NOT have to contain an entire complete DATA step program to accomplish what you want to do. But this also means that your macro invocation might have to change.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 29 Jul 2010 16:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57541#M12418</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-07-29T16:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57542#M12419</link>
      <description>when you pass var1 = 200907 then only k_200907 is created.&lt;BR /&gt;
the next time k_200907 will be over written by other values that you pass(200908,200909).&lt;BR /&gt;
so at any given time with your logic only one variable will be created.&lt;BR /&gt;
&lt;BR /&gt;
try to pass three parameters and var1=, var2=, var3= and add the logic in the code in the if-then else statement.</description>
      <pubDate>Thu, 29 Jul 2010 18:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57542#M12419</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-07-29T18:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57543#M12420</link>
      <description>Many thanks Cynthia</description>
      <pubDate>Sat, 31 Jul 2010 01:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57543#M12420</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-31T01:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57544#M12421</link>
      <description>Many thanks SAS Phile</description>
      <pubDate>Sat, 31 Jul 2010 01:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-within-a-data-step/m-p/57544#M12421</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-31T01:29:06Z</dc:date>
    </item>
  </channel>
</rss>

