<?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: Inserting additional rows based on other rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Inserting-additional-rows-based-on-other-rows/m-p/615910#M180213</link>
    <description>&lt;P&gt;Hi elbarto,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps not the optimal solution, and I am also not able to follow 100% the steps that you want to perform. But would something like the below help you to get the "new" rows that you want to include?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the below helps, then afterwards either with RETAIN statement or LAG function you should be able to derive the new variables that you want as needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please check as an example.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Identify unique firm ID, Location ID and year values*/
proc sort data = have nodupkey out = x (keep = firm_id location_id year);
  by firm_id location_id year;
  where operate = 0;
run;

/*Per firm/location ID keep only first (start) and last (end) year*/
data x2;
set x;
  by firm_id location_id;
  if first.location_id or last.location_id;
run;

/*Transpose so that per firm/location ID start and end year are on same row*/
proc transpose data = x2 out = x3 (drop = _name_ rename = (col1 = start col2 = end));
  var year;
  by firm_id location_id;
run;

/*Create a "skeleton" with all years from start to end per firm/location ID*/
data x4 (drop = start end i);
set x3;
  by firm_id location_id;
  /*For the case of only single year (i.e. end = start)*/
  if end = . then end = start;
  do i = start to end by 1;
    year = i;
    output;
  end;
run;

/*Merge "skeleton" to original data by firm/location ID and year*/
data have2;
merge have (in = A) x4 (in = B);
  by firm_id location_id year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 08 Jan 2020 13:04:50 GMT</pubDate>
    <dc:creator>geoskiad</dc:creator>
    <dc:date>2020-01-08T13:04:50Z</dc:date>
    <item>
      <title>Inserting additional rows based on other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-additional-rows-based-on-other-rows/m-p/615591#M180065</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input year	firm_id	location_id	action	action_amount	operate	new_entry
;
cards;
2013	28013	6085	1	10000	0	0
2015	28013	6085	1	12000	0	0
2015	28013	29189	1	10000	0	0
2016	28013	34019	1	5000	1	1
2017	28013	34019	0	0	1	2
2011	120609	9003	1	7000	0	0
2012	120609	9003	0	0	1	1
2013	120609	9003	1	5000	1	2
2012	247908	23001	1	9000	0	0
2013	247908	23001	1	8000	0	0
2014	247908	23001	1	8500	1	1
2015	247908	23001	0	0	1	2
2003	356123	1001	0	0	0	0
2004	356123	1001	0	0	0	0
2009	356123	1001	1	9800	1	1



;
run;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;I want to add additional rows and two new variables called "pre_action" and "pre_action_amount" to obtain the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
input year	firm_id	location_id	action	action_amount	operate	new_entry	pre_action	pre_action_amount
;
cards;


2013	28013	6085	1	10000	0	0	.	.
2014	28013	6085	0	0	0	0	1	10000
2015	28013	6085	1	12000	0	0	.	.
2016	28013	6085	0	0	0	0	1	12000
2015	28013	29189	1	6500	0	0	.	.
2016	28013	29189	0	0	0	0	1	6500
2016	28013	34019	1	5000	1	1	0	0
2017	28013	34019	0	0	1	2	.	.
2011	120609	9003	1	7000	0	0	.	.
2012	120609	9003	0	0	1	1	1	7000
2013	120609	9003	1	5000	1	2	.	.
2012	247908	23001	1	9000	0	0	.	.
2013	247908	23001	1	8000	0	0	1	9000
2014	247908	23001	1	8500	1	1	1	8000
2015	247908	23001	0	0	1	2	.	.
2003	356123	1001	0	0	0	0	.	.
2004	356123	1001	0	0	0	0	0	0
2005	356123	1001	0	0	0	0	0	0
2009	356123	1001	1	9800	1	1	0	0



;
run;&lt;/PRE&gt;&lt;P&gt;The rules are as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) First, consider only the rows with operate = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;For each firm_id and location_id pair, if in the following year there is no row with the same firm_id and location_id, then create a new row with the following year and same firm_id and location_id pair. The variables action, action_amount, operate, and new_entry are all set to 0, while pre_action and pre_action_amount is set to be the value of action and action_amount in the previous year. Example: In year 2013, for the firm_id/location_id pair 28013/6085, we have operate = 0. But in 2014, there are no observations for this firm_id/location_id pair. So we set action, action_amount, operate, and new_entry to be 0 and pre_action=1 and pre_action_amount=10000 which are the values for action and action_amount in 2013.&lt;/LI&gt;&lt;LI&gt;For each firm_id and location_id pair, if in the following year there is a row with the same firm_id and location_id, then simply set pre_action and pre_action_amount to be the value of action and action_amount in the previous year. Example: In year 2011 for firm_id/location_id 120609/9003, we have operate=0. But in the next year 2012, there is a row with this firm_id/location_id pair. So we set pre_action=1 and pre_action_amount=7000 which are the values for action and action_amount in 2011. Another example is in year 2003, for the firm_id/location_id 356123/1001.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;2) Now consider the rows with new_entry=1 that do not yet have a value of pre_action and pre_action_amount. Set both pre_action and pre_action_amount to be 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) All other values of pre_action and pre_action_amount are empty.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 08:28:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-additional-rows-based-on-other-rows/m-p/615591#M180065</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2020-01-07T08:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting additional rows based on other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-additional-rows-based-on-other-rows/m-p/615910#M180213</link>
      <description>&lt;P&gt;Hi elbarto,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps not the optimal solution, and I am also not able to follow 100% the steps that you want to perform. But would something like the below help you to get the "new" rows that you want to include?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the below helps, then afterwards either with RETAIN statement or LAG function you should be able to derive the new variables that you want as needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please check as an example.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Identify unique firm ID, Location ID and year values*/
proc sort data = have nodupkey out = x (keep = firm_id location_id year);
  by firm_id location_id year;
  where operate = 0;
run;

/*Per firm/location ID keep only first (start) and last (end) year*/
data x2;
set x;
  by firm_id location_id;
  if first.location_id or last.location_id;
run;

/*Transpose so that per firm/location ID start and end year are on same row*/
proc transpose data = x2 out = x3 (drop = _name_ rename = (col1 = start col2 = end));
  var year;
  by firm_id location_id;
run;

/*Create a "skeleton" with all years from start to end per firm/location ID*/
data x4 (drop = start end i);
set x3;
  by firm_id location_id;
  /*For the case of only single year (i.e. end = start)*/
  if end = . then end = start;
  do i = start to end by 1;
    year = i;
    output;
  end;
run;

/*Merge "skeleton" to original data by firm/location ID and year*/
data have2;
merge have (in = A) x4 (in = B);
  by firm_id location_id year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-additional-rows-based-on-other-rows/m-p/615910#M180213</guid>
      <dc:creator>geoskiad</dc:creator>
      <dc:date>2020-01-08T13:04:50Z</dc:date>
    </item>
  </channel>
</rss>

