<?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: Using macro to create new column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709079#M26679</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358095"&gt;@jlin4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I am trying to make use of macro to create a column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the dataset below, I would like to create a new column "name3". Specifically, if class=1, then name3=name1, and if class=2, then name3=name2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;&lt;BR /&gt;input name1$ name2$ class$;&lt;BR /&gt;cards;&lt;BR /&gt;adam billy a&lt;BR /&gt;charli dan b&lt;BR /&gt;eliot francis a&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;I tried to do so with the following codes. However, name3 created is always equal to name2.&lt;/P&gt;
&lt;P&gt;May I ask if there is a way in which I could modify my codes to get the desired results? Thank you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		%if &amp;amp;cl="a" %then %do;
			name3 = &amp;amp;n1;
		%end;
		
		%else %if &amp;amp;cl="b" %then %do;
			name3 = &amp;amp;n2;
		%end;
	run;
%mend;

%pickname(tb=test, cl=class, n1=name1, n2=name2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please show your log. When I run your exact as posted code the variable name3 is not created at all.&lt;/P&gt;
&lt;P&gt;With option Mprint set to run the first time:&lt;/P&gt;
&lt;PRE&gt;201  %macro pickname(tb=, cl=, n1=, n2=);
202     data &amp;amp;tb; set &amp;amp;tb;
203        %if &amp;amp;cl="a" %then %do;
204           name3 = &amp;amp;n1;
205        %end;
206
207        %else %if &amp;amp;cl="b" %then %do;
208           name3 = &amp;amp;n2;
209        %end;
210     run;
211  %mend;
212
213  options mprint;
214
215  %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME):   data test;
MPRINT(PICKNAME):   set test;
MPRINT(PICKNAME):   run;

NOTE: There were 3 observations read from the data set
      WORK.TEST.
NOTE: The data set WORK.TEST has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

&lt;/PRE&gt;
&lt;P&gt;This non-assignment happens because this code:&lt;/P&gt;
&lt;P&gt;%if &amp;amp;cl="a" %then %do;&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;%if &amp;amp;cl="b" %then %do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;are &lt;STRONG&gt;never&lt;/STRONG&gt; true. The values you have provided for Cl is class, without quotes. So class is not equal to "a" or "b"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro %if cannot use the values the values of data step variables. So you may have wanted&lt;/P&gt;
&lt;PRE&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		if &amp;amp;cl="a" then do;
			name3 = &amp;amp;n1;
		end;
		
		else if &amp;amp;cl="b" then do;
			name3 = &amp;amp;n2;
		end;
	run;
%mend;&lt;/PRE&gt;
&lt;P&gt;Before attempting a macro have actual code that works without any macro variables or statements.&lt;/P&gt;
&lt;P&gt;Then replace the bits, often one at a time, to make sure things work while testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One suspects that you THOUGHT Name3=Name 2 because at some earlier step you had different logic that assigned that value and did not return to using the actual original Test data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second hint: The code structure of&lt;/P&gt;
&lt;P&gt;Data name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is very poor for testing recoding. It completely replaces the source data set unless there are errors that stop the step from running at all. So when you retest code the next time the not original variables now exist the data set and you cannot compare the start to the output data.&lt;/P&gt;
&lt;P&gt;Third hint:&lt;/P&gt;
&lt;P&gt;Learn Options MPRINT SYMBOLGEN LOGIC;&lt;/P&gt;
&lt;P&gt;to create details of the code and program flow inside a macro.&lt;/P&gt;</description>
    <pubDate>Sat, 02 Jan 2021 18:13:16 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-01-02T18:13:16Z</dc:date>
    <item>
      <title>Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709074#M26676</link>
      <description>&lt;P&gt;Hi, I am trying to make use of macro to create a column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the dataset below, I would like to create a new column "name3". Specifically, if class=1, then name3=name1, and if class=2, then name3=name2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;&lt;BR /&gt;input name1$ name2$ class$;&lt;BR /&gt;cards;&lt;BR /&gt;adam billy a&lt;BR /&gt;charli dan b&lt;BR /&gt;eliot francis a&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;I tried to do so with the following codes. However, name3 created is always equal to name2.&lt;/P&gt;&lt;P&gt;May I ask if there is a way in which I could modify my codes to get the desired results? Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		%if &amp;amp;cl="a" %then %do;
			name3 = &amp;amp;n1;
		%end;
		
		%else %if &amp;amp;cl="b" %then %do;
			name3 = &amp;amp;n2;
		%end;
	run;
%mend;

%pickname(tb=test, cl=class, n1=name1, n2=name2);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 17:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709074#M26676</guid>
      <dc:creator>jlin4</dc:creator>
      <dc:date>2021-01-02T17:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709078#M26678</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358095"&gt;@jlin4&lt;/a&gt;&amp;nbsp; Your %IF macro code would evaluate to False conditional check and so there wouldn't be any texts generated that can be sent to the compiler as a part of datastep execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IF condition-&amp;gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;amp;cl="a"&lt;/STRONG&gt; resolving to&amp;nbsp;&lt;STRONG&gt; class="a"&lt;/STRONG&gt; is false&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and likewise class="b" is also false&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please review your logic&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 18:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709078#M26678</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-02T18:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709079#M26679</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358095"&gt;@jlin4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I am trying to make use of macro to create a column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the dataset below, I would like to create a new column "name3". Specifically, if class=1, then name3=name1, and if class=2, then name3=name2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;&lt;BR /&gt;input name1$ name2$ class$;&lt;BR /&gt;cards;&lt;BR /&gt;adam billy a&lt;BR /&gt;charli dan b&lt;BR /&gt;eliot francis a&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;I tried to do so with the following codes. However, name3 created is always equal to name2.&lt;/P&gt;
&lt;P&gt;May I ask if there is a way in which I could modify my codes to get the desired results? Thank you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		%if &amp;amp;cl="a" %then %do;
			name3 = &amp;amp;n1;
		%end;
		
		%else %if &amp;amp;cl="b" %then %do;
			name3 = &amp;amp;n2;
		%end;
	run;
%mend;

%pickname(tb=test, cl=class, n1=name1, n2=name2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please show your log. When I run your exact as posted code the variable name3 is not created at all.&lt;/P&gt;
&lt;P&gt;With option Mprint set to run the first time:&lt;/P&gt;
&lt;PRE&gt;201  %macro pickname(tb=, cl=, n1=, n2=);
202     data &amp;amp;tb; set &amp;amp;tb;
203        %if &amp;amp;cl="a" %then %do;
204           name3 = &amp;amp;n1;
205        %end;
206
207        %else %if &amp;amp;cl="b" %then %do;
208           name3 = &amp;amp;n2;
209        %end;
210     run;
211  %mend;
212
213  options mprint;
214
215  %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME):   data test;
MPRINT(PICKNAME):   set test;
MPRINT(PICKNAME):   run;

NOTE: There were 3 observations read from the data set
      WORK.TEST.
NOTE: The data set WORK.TEST has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

&lt;/PRE&gt;
&lt;P&gt;This non-assignment happens because this code:&lt;/P&gt;
&lt;P&gt;%if &amp;amp;cl="a" %then %do;&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;%if &amp;amp;cl="b" %then %do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;are &lt;STRONG&gt;never&lt;/STRONG&gt; true. The values you have provided for Cl is class, without quotes. So class is not equal to "a" or "b"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro %if cannot use the values the values of data step variables. So you may have wanted&lt;/P&gt;
&lt;PRE&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		if &amp;amp;cl="a" then do;
			name3 = &amp;amp;n1;
		end;
		
		else if &amp;amp;cl="b" then do;
			name3 = &amp;amp;n2;
		end;
	run;
%mend;&lt;/PRE&gt;
&lt;P&gt;Before attempting a macro have actual code that works without any macro variables or statements.&lt;/P&gt;
&lt;P&gt;Then replace the bits, often one at a time, to make sure things work while testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One suspects that you THOUGHT Name3=Name 2 because at some earlier step you had different logic that assigned that value and did not return to using the actual original Test data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second hint: The code structure of&lt;/P&gt;
&lt;P&gt;Data name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is very poor for testing recoding. It completely replaces the source data set unless there are errors that stop the step from running at all. So when you retest code the next time the not original variables now exist the data set and you cannot compare the start to the output data.&lt;/P&gt;
&lt;P&gt;Third hint:&lt;/P&gt;
&lt;P&gt;Learn Options MPRINT SYMBOLGEN LOGIC;&lt;/P&gt;
&lt;P&gt;to create details of the code and program flow inside a macro.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 18:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709079#M26679</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-02T18:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709080#M26680</link>
      <description>&lt;P&gt;Before designing the macro first create (or envision) the SAS code you want it to create.&lt;/P&gt;
&lt;P&gt;Sounds like you want to create this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set test;
  if class="a" then name3 = name1;
  else if class="b" then name3 = name2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test it make sure it works (do you really want to destroy the original TEST dataset?)&lt;/P&gt;
&lt;P&gt;Next figure out want parts you want to make dynamic&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="courier new,courier"&gt;data &lt;FONT color="#FF0000"&gt;test&lt;/FONT&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set &lt;FONT color="#FF0000"&gt;test&lt;/FONT&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; if &lt;FONT color="#FF0000"&gt;class&lt;/FONT&gt;="a" then name3 = &lt;FONT color="#FF0000"&gt;name1&lt;/FONT&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; else if &lt;FONT color="#FF0000"&gt;class&lt;/FONT&gt;="b" then name3 = &lt;FONT color="#FF0000"&gt;name2&lt;/FONT&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;and replace with macro variable references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let tb=test;
%let cl=class;
%let n1=name1;
%let n2=name2;
data &amp;amp;tb;
  set &amp;amp;tb;
  if &amp;amp;cl="a" then name3 = &amp;amp;n1;
  else if &amp;amp;cl="b" then name3 = &amp;amp;n2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test it to make sure it works.&lt;/P&gt;
&lt;P&gt;Now convert it to a macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb,cl,n1,n2);
data &amp;amp;tb;
  set &amp;amp;tb;
  if &amp;amp;cl="a" then name3 = &amp;amp;n1;
  else if &amp;amp;cl="b" then name3 = &amp;amp;n2;
run;
%mend ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And test it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
%pickname(tb=test, cl=class, n1=name1, n2=name2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 18:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709080#M26680</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-02T18:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709081#M26681</link>
      <description>&lt;P&gt;Macro program is just a generator of code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check line:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%if &amp;amp;cl="a" &lt;/LI-CODE&gt;
&lt;P&gt;suppose &amp;amp;cl resolve as&amp;nbsp;&lt;STRONG&gt;a.&amp;nbsp;&lt;/STRONG&gt;then you compare&amp;nbsp;&lt;STRONG&gt;a&lt;/STRONG&gt; to&amp;nbsp;&lt;STRONG&gt;"a"&lt;/STRONG&gt; which is false.&lt;/P&gt;
&lt;P&gt;If you meant to compare the &lt;STRONG&gt;value&lt;/STRONG&gt; of a variable given by &lt;STRONG&gt;&amp;amp;cl&lt;/STRONG&gt;&amp;nbsp;then don't do it by a macro&lt;/P&gt;
&lt;P&gt;but just by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If &amp;lt;variable name&amp;gt; = "a" ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that what you want to do is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 set test;
       if class = "a" then name3 = name1; else
       if class = "b" then name3 = name2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Suppose you want to replicate it for vaious tables with different variable names then you can do it with next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		if &amp;amp;cl = "a" then name3 = &amp;amp;n1; else
		if &amp;amp;cl = "b" then name3 = &amp;amp;n2;
	run;
%mend;

%pickname(tb=test, cl=class, n1=name1, n2=name2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 18:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709081#M26681</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-02T18:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using macro to create new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709082#M26682</link>
      <description>&lt;P&gt;When developing and debugging macro code, it's crucial to turn on the option MPRINT and review your log, so that you can see the SAS code that the macro generated.&amp;nbsp; When I do that with your code, I get:&lt;/P&gt;
&lt;PRE&gt;22   options mprint ;
23   %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME):   data test;
MPRINT(PICKNAME):   set test;
MPRINT(PICKNAME):   run;
&lt;/PRE&gt;
&lt;P&gt;You can see that there is no assignment statement. Why not?&amp;nbsp; Because both %IF statements evaluate to false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you are trying to evaluate the value of a data step variable, and conditionally execute a data step assignment statement.&amp;nbsp; To do that, you can use the data step IF statement, not the macro %IF statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do that, your code will look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pickname(tb=, cl=, n1=, n2=);
	data &amp;amp;tb; set &amp;amp;tb;
		if &amp;amp;cl="a" then do;
			name3 = &amp;amp;n1;
		end;
		
		else if &amp;amp;cl="b" then do;
			name3 = &amp;amp;n2;
		end;
	run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And when you run it, the log will show that the macro generated a data step which looks like what you want:&lt;/P&gt;
&lt;PRE&gt;23   %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME):   data test;
MPRINT(PICKNAME):   set test;
MPRINT(PICKNAME):   if class="a" then do;
MPRINT(PICKNAME):   name3 = name1;
MPRINT(PICKNAME):   end;
MPRINT(PICKNAME):   else if class="b" then do;
MPRINT(PICKNAME):   name3 = name2;
MPRINT(PICKNAME):   end;
MPRINT(PICKNAME):   run;
&lt;/PRE&gt;
&lt;P&gt;As you work with the macro language, it's important to understand (and remember) the difference between %IF (a macro language statement for evaluating macro variable expressions) and IF (a data step statement for evaluating data step variable expressions), %DO vs DO, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 18:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-macro-to-create-new-column/m-p/709082#M26682</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-01-02T18:25:22Z</dc:date>
    </item>
  </channel>
</rss>

