<?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: Creating a new variable based on another column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-another-column/m-p/705541#M216473</link>
    <description>&lt;P&gt;What have you tried so far?&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
	set have;
	by section id;
	
	retain _action;
	
	if first.id then _action = .;
	
	if not missing(_action) and action = 1 then do;
		event = 1;
		_action = .;
	end;
	
	if not missing(type) then do;
		if action = 1 then do;
			event = 1;
			_action = .;
		end;
		else do;
			_action = 0;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 13 Dec 2020 19:33:16 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-12-13T19:33:16Z</dc:date>
    <item>
      <title>Creating a new variable based on another column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-another-column/m-p/705523#M216465</link>
      <description>&lt;P&gt;Hi everyone, I am trying to create a new variable based on another column. Here is an example of the HAVE data.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have ;
  input section	$ id	year	action	type $;
DATALINES;
first	8069	2002	0	.
first	8069	2003	0	.
first	8069	2004	0	ann1
first	8069	2005	1	.
first	8069	2006	1	.
first	8069	2007	1	.
first	8234	1988	0	.
first	8234	1989	0	.
first	8234	1990	0	.
first	8234	1991	1	ann1
first	8234	1992	0	.
first	8234	1993	0	.
first	8234	1994	0	ann2
first	8234	1995	1	.
first	8234	1996	1	.
first	8234	1997	1	.
first	8234	1998	0	.
first	8234	1999	0	.
second	1032	2011	1	ann1
second	1032	2012	0	.
second	1032	2013	0	.
second	1032	2014	0	.
second	8069	2005	0	.
second	8069	2006	0	ann1
second	8069	2007	0	.
second	8069	2008	0	ann2
second	8069	2009	1	.
second	8069	2010	1	.
second	8234	1999	0	.
second	8234	2000	0	.
second	8234	2001	0	ann1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is an example of the WANT data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want ;
  input section	$ id	year	action	type $ event;
DATALINES;
first	8069	2002	0	.	.
first	8069	2003	0	.	.
first	8069	2004	0	ann1	.
first	8069	2005	1	.	1
first	8069	2006	1	.	.
first	8069	2007	1	.	.
first	8234	1988	0	.	.
first	8234	1989	0	.	.
first	8234	1990	0	.	.
first	8234	1991	1	ann1	1
first	8234	1992	0	.	.
first	8234	1993	0	.	.
first	8234	1994	0	ann2	.
first	8234	1995	1	.	1
first	8234	1996	1	.	.
first	8234	1997	1	.	.
first	8234	1998	0	.	.
first	8234	1999	0	.	.
second	1032	2011	1	ann1	1
second	1032	2012	0	.	.
second	1032	2013	0	.	.
second	1032	2014	0	.	.
second	8069	2005	0	.	.
second	8069	2006	0	ann1	.
second	8069	2007	0	.	.
second	8069	2008	0	ann2	.
second	8069	2009	1	.	1
second	8069	2010	1	.	.
second	8234	1999	0	.	.
second	8234	2000	0	.	.
second	8234	2001	0	ann1	.
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I want to do is as follows. For each section and id, look at the "type" variable. If it is a nonmissing value, then look at the corresponding "action" variable in that year. If this action value is:&lt;/P&gt;&lt;P&gt;i) 0 &lt;EM&gt;and&lt;/EM&gt; the &lt;EM&gt;following &lt;/EM&gt;year's action value is 1, then set event = 1 for the following year;&lt;/P&gt;&lt;P&gt;ii) 0 &lt;EM&gt;and&lt;/EM&gt; the &lt;EM&gt;following&lt;/EM&gt; year's action value is 0, then set event = . for the following year;&lt;/P&gt;&lt;P&gt;iii) 1, then set event = 1 for the same year, regardless of what the value of action is in the following year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All other event values are set to missing. Also, in cases where there is a nonmissing value for type and action = 0 for that year, but there is no data for the following year, then simply set event = . for the same year (see the last row in want).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: action only ever takes on the value 0 or 1. Type also only ever takes on the values ann1 or ann2, but this does not really matter, as long as it is a nonmissing value then the above rules apply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 18:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-another-column/m-p/705523#M216465</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2020-12-13T18:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable based on another column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-another-column/m-p/705541#M216473</link>
      <description>&lt;P&gt;What have you tried so far?&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
	set have;
	by section id;
	
	retain _action;
	
	if first.id then _action = .;
	
	if not missing(_action) and action = 1 then do;
		event = 1;
		_action = .;
	end;
	
	if not missing(type) then do;
		if action = 1 then do;
			event = 1;
			_action = .;
		end;
		else do;
			_action = 0;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 19:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-another-column/m-p/705541#M216473</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-13T19:33:16Z</dc:date>
    </item>
  </channel>
</rss>

