<?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 Need to add column in dataset based on If condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463846#M118225</link>
    <description>&lt;P&gt;I have one sas dataset so want to check of that value and column_position not exist then add that obs in dataset else if value of column is not exist and column_position is same then replace that col_position (for example at 15th row there is one observation so now I want to add new value to 15th row then original 15th row goes to 16th obs , 16th obs goes to 17th...etc (&lt;EM&gt;n&lt;/EM&gt;+1 r lag function not sure)&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;I am trying this, but not working properly, please help me&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data test;
input value $ data_type $ length col_pos;
datalines;
harry varchar 100 1
henry number 100 2
homry varchar 100 3
;

%macro add_column(value,data_type,length,col_pos);

data _null_;
set test;
if value="&amp;amp;value" then call symput('col_name',1)
else call symput('col_name',2)
if col_pos="&amp;amp;col_pos" then call symput ('col_pos',1)
else call symput('col_pos,2)
run;

if &amp;amp;col_name=2 and &amp;amp;col_pos=2 then;
%do;
  data test;
set test;
value="&amp;amp;value";
data_type="&amp;amp;data_type";
length="&amp;amp;length";
col_pos="&amp;amp;col_pos";
output;
run;
%end;
%mend;
%add_column(alex,varchar,100,4);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Suppose if value changed and col_pos same For example, I need to replace 2nd col_pos and 2nd col_pos from test dataset above should go to 3rd %add_column(joe,varchar,100,2); if &amp;amp;col_name=2(as value is different) and &amp;amp;col_pos=1(col_pos is same i.e 2) then; (I am trying to write this but didn't worked) '2' col_pos is same&lt;/P&gt;&lt;P&gt;Please help me with this&lt;/P&gt;</description>
    <pubDate>Mon, 21 May 2018 18:32:31 GMT</pubDate>
    <dc:creator>harry_87</dc:creator>
    <dc:date>2018-05-21T18:32:31Z</dc:date>
    <item>
      <title>Need to add column in dataset based on If condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463846#M118225</link>
      <description>&lt;P&gt;I have one sas dataset so want to check of that value and column_position not exist then add that obs in dataset else if value of column is not exist and column_position is same then replace that col_position (for example at 15th row there is one observation so now I want to add new value to 15th row then original 15th row goes to 16th obs , 16th obs goes to 17th...etc (&lt;EM&gt;n&lt;/EM&gt;+1 r lag function not sure)&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;I am trying this, but not working properly, please help me&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data test;
input value $ data_type $ length col_pos;
datalines;
harry varchar 100 1
henry number 100 2
homry varchar 100 3
;

%macro add_column(value,data_type,length,col_pos);

data _null_;
set test;
if value="&amp;amp;value" then call symput('col_name',1)
else call symput('col_name',2)
if col_pos="&amp;amp;col_pos" then call symput ('col_pos',1)
else call symput('col_pos,2)
run;

if &amp;amp;col_name=2 and &amp;amp;col_pos=2 then;
%do;
  data test;
set test;
value="&amp;amp;value";
data_type="&amp;amp;data_type";
length="&amp;amp;length";
col_pos="&amp;amp;col_pos";
output;
run;
%end;
%mend;
%add_column(alex,varchar,100,4);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Suppose if value changed and col_pos same For example, I need to replace 2nd col_pos and 2nd col_pos from test dataset above should go to 3rd %add_column(joe,varchar,100,2); if &amp;amp;col_name=2(as value is different) and &amp;amp;col_pos=1(col_pos is same i.e 2) then; (I am trying to write this but didn't worked) '2' col_pos is same&lt;/P&gt;&lt;P&gt;Please help me with this&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 18:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463846#M118225</guid>
      <dc:creator>harry_87</dc:creator>
      <dc:date>2018-05-21T18:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Need to add column in dataset based on If condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463860#M118234</link>
      <description>&lt;P&gt;It may help to provide a small example of what you expect the output to look like.&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 19:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463860#M118234</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-21T19:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: Need to add column in dataset based on If condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463957#M118268</link>
      <description>&lt;P&gt;Sorry, Actually I need to update dataset, I dont want to add new column (in short add rows/obs in a dataset)&lt;/P&gt;&lt;P&gt;If I execute below macro&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%add_column(alex,varchar,100,4);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then output would be&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;U&gt;Value Datatype length col_pos&lt;/U&gt;&lt;BR /&gt;harry varchar 100 1
henry number 100 2
homry varchar 100 3&lt;BR /&gt;&lt;/CODE&gt;alex varchar 100 4&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; and If I execute below macro ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%add_column(joe,varchar,100,2);&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;then output would be&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;U&gt;Value Datatype length col_pos&lt;/U&gt;&lt;BR /&gt;harry varchar 100 1&lt;BR /&gt;joe varchar 100 2
henry number 100 3
homry varchar 100 4&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 05:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/463957#M118268</guid>
      <dc:creator>harry_87</dc:creator>
      <dc:date>2018-05-22T05:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Need to add column in dataset based on If condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/464106#M118326</link>
      <description>&lt;P&gt;Your data example implies the col_pos variable is an order in the data set variable and unique-one record only with a given value.. As such we could use that to UPDATE with BY col_pos as long as the data set is actually sorted by col_pos. The datastep update statement will add new records when the by variable value is not in the base data set.&lt;/P&gt;
&lt;P&gt;Consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro add_column(value,data_type,length,col_pos);
/* get a single record to have the variable types/ format/lenghts*/
data work.temp;
   set test (obs=1);
run;
/* set the values from the parameters*/
data work.temp;
   set work.temp;
   value="&amp;amp;value.";
   data_type="&amp;amp;data_type.";
   length=&amp;amp;length.;
   col_pos=&amp;amp;col_pos.;
   output;
run;
/* update the data set*/
data test;
   update test
          work.temp;
   by col_pos;
run;

%mend;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 May 2018 15:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-add-column-in-dataset-based-on-If-condition/m-p/464106#M118326</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-22T15:36:20Z</dc:date>
    </item>
  </channel>
</rss>

