<?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 create a variable within a macro based on a macro variable condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890693#M351936</link>
    <description>&lt;P&gt;We can remove that 'keep' line and keep all variables. the problem came up for the 2 variables that i wanted to create using marco variables. I want the real variable, not the variable name. &lt;span class="lia-unicode-emoji" title=":flushed_face:"&gt;😳&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Aug 2023 03:52:39 GMT</pubDate>
    <dc:creator>stataq</dc:creator>
    <dc:date>2023-08-24T03:52:39Z</dc:date>
    <item>
      <title>How to create a variable within a macro based on a macro variable condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890691#M351934</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;am new to sas marco. I try to define new variable&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;SOURCE_DT&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;SOURCE_TIM&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;within macro but fail.&lt;/P&gt;&lt;P&gt;Could someone guide me on this? Please pardon me if the errors below are too silly.&lt;span class="lia-unicode-emoji" title=":flushed_face:"&gt;😳&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;`timvar `will be the var(A) in `indata``` that looks like HH:MM.&amp;nbsp; I want to create var source_tim=A.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%macro m_test(indata, outdata, dtvar=.,  timvar=.); 

data &amp;amp;outdata.;
    set &amp;amp;indata.(encoding=any);

    SOURCE_TIM=.;
    %if (&amp;amp;timvar. ne .) %then %do;
    SOURCE_TIM= &amp;amp;timvar. ;
    %END;

    SOUCE_DT=&amp;amp;dtvar.;

keep SOURCE_TIM SOURCE_DT;
RUN;
%mend m_test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2023 03:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890691#M351934</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2023-08-24T03:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable within a macro based on a macro variable condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890692#M351935</link>
      <description>&lt;P&gt;Why are you dropping all of the other variables?&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2023 03:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890692#M351935</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-24T03:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable within a macro based on a macro variable condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890693#M351936</link>
      <description>&lt;P&gt;We can remove that 'keep' line and keep all variables. the problem came up for the 2 variables that i wanted to create using marco variables. I want the real variable, not the variable name. &lt;span class="lia-unicode-emoji" title=":flushed_face:"&gt;😳&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2023 03:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890693#M351936</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2023-08-24T03:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable within a macro based on a macro variable condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890694#M351937</link>
      <description>&lt;P&gt;Show an actual example dataset that is the input.&lt;/P&gt;
&lt;P&gt;Show the SAS code you want to generate (forget about make a macro or using macro variables for now).&lt;/P&gt;
&lt;P&gt;Show the dataset you want to get from the input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Explain what parts of the code needs to change for other datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you might use SASHELP.CLASS as the input data and create a new variable named AGE2 that is the square of the value of age.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class;
  age2 = age ** 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if you wanted to make a macro that could could generate AGE2 using different formulas you could first create a macro variable, call it say FORMULA.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let formula=age ** 2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then change the data step to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class;
  age2 = &amp;amp;formula ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can now convert that to a macro with FORMULA as one of its parameters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(formula);
data want;
  set sashelp.class;
  age2 = &amp;amp;formula ;
run;
%mend mymacro;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you could then call like this to generate the same data step we started with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(formula=age ** 2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you could also pass something else.&amp;nbsp; For example if you wanted AGE2 to be 21 for everyone you could just use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(formula=21)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps you really want AGE2 to be a character variable.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(formula="Age of Student")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(formula=put(age,Z3.))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So what data step do you want to run? Once you know that you can begin to try and make a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example for your macro if you wanted&amp;nbsp;SOURCE_TIM to be set to 11 AM you might call it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%m_test(indata=sashelp.class, outdata=want, dtvar=.,  timvar='11:00't); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that it generates the statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SOURCE_TIM='11:00't ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 04:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-within-a-macro-based-on-a-macro/m-p/890694#M351937</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-24T04:14:50Z</dc:date>
    </item>
  </channel>
</rss>

