<?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 new variables using result of an existing variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623689#M183654</link>
    <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id count result;
	cards;
1 1 0
1 2 0
1 3 1
1 4 0
1 5 0
1 6 1
2 1 0
2 2 0
3 1 1
4 1 0
4 2 0
4 3 1
5 1 1
5 2 0
5 3 1
;
proc sql;
	create table res1 as
		select id, min(count) as count1
		from t
			where result=1
				group by id;
quit;

data want;
	merge have res1;
	by id;
	if count&amp;lt;count1 then
		test_before = 'yes';
	else if count&amp;gt;count1 then
		test_after = 'yes';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I create an intermediate set with al id's and the count at which the result was 1 for the first time. Then I left-join that with the original set to determine if a row was before or after the first result=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This all assumes the data was sorted by ID and Count. If not you need to add an additional sort step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And yes, with some effort this can be rewritten in a single SQL. As usual, many roads lead to Rome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
    <pubDate>Mon, 10 Feb 2020 20:34:20 GMT</pubDate>
    <dc:creator>jklaverstijn</dc:creator>
    <dc:date>2020-02-10T20:34:20Z</dc:date>
    <item>
      <title>Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623655#M183643</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I want to identify tests that are done before 1st positive Result (i.e. all tests before 1st Result = 1) and all tests done after 1st positive Result (i.e. all tests done after 1st result = 1) as two separate variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I need for the new variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Count&amp;nbsp; &amp;nbsp; Result&amp;nbsp; &amp;nbsp; &amp;nbsp;Test_Before&amp;nbsp; &amp;nbsp;Test_After&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&amp;nbsp;&amp;nbsp;&lt;/P&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;P&gt;Thank you so much in advance for all the responses. I really appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SM&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2020 18:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623655#M183643</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2020-02-10T18:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623684#M183650</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Count Result;
datalines;
1 1 0 
1 2 0 
1 3 1 
1 4 0 
1 5 0 
1 6 1 
2 1 0 
2 2 0 
3 1 1 
4 1 0 
4 2 0 
4 3 1 
5 1 1 
5 2 0 
5 3 1 
;

data want(drop=_:);
   _f=0;
   do until (last.id);
      set have;
      by id;
      if result=1 &amp;amp; _f=0 then do;
         _count=count; _f=1;
      end;
   end;
   do until (last.id);
      set have;
      length Test_Before Test_After $3;
      Test_Before = '';
      Test_After = '';
      by id;
      if _count = . then do;
         Test_Before = '';
         Test_After = '';
      end;
      else if count &amp;lt; _count then Test_Before = 'Yes';
      else if count &amp;gt; _count then Test_After = 'Yes';
      output;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2020 20:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623684#M183650</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-02-10T20:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623689#M183654</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id count result;
	cards;
1 1 0
1 2 0
1 3 1
1 4 0
1 5 0
1 6 1
2 1 0
2 2 0
3 1 1
4 1 0
4 2 0
4 3 1
5 1 1
5 2 0
5 3 1
;
proc sql;
	create table res1 as
		select id, min(count) as count1
		from t
			where result=1
				group by id;
quit;

data want;
	merge have res1;
	by id;
	if count&amp;lt;count1 then
		test_before = 'yes';
	else if count&amp;gt;count1 then
		test_after = 'yes';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I create an intermediate set with al id's and the count at which the result was 1 for the first time. Then I left-join that with the original set to determine if a row was before or after the first result=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This all assumes the data was sorted by ID and Count. If not you need to add an additional sort step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And yes, with some effort this can be rewritten in a single SQL. As usual, many roads lead to Rome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2020 20:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623689#M183654</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2020-02-10T20:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623708#M183660</link>
      <description>&lt;P&gt;Alternatively..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=rc _:);
   if _N_=1 then do;
      declare hash h ();
      h.definekey ('id');
      h.definedata ('id','_count');
      h.definedone();
      
      do until (lr);
         set have(rename=count=_count) end=lr;
         if Result=1 then h.ref();
      end;
   end;

   set have;
   _count=.;

   if h.find()=0 then do;
      if      count &amp;lt; _count then Test_Before = 'Yes';
      else if count &amp;gt; _count then Test_After = 'Yes';
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID Result Count Test_Before Test_After 
1  0      1     Yes   
1  0      2     Yes   
1  1      3         
1  0      4                 Yes 
1  0      5                 Yes 
1  1      6                 Yes 
2  0      1         
2  0      2         
3  1      1         
4  0      1     Yes   
4  0      2     Yes   
4  1      3         
5  1      1         
5  0      2                 Yes 
5  1      3                 Yes &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2020 21:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623708#M183660</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-02-10T21:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623710#M183662</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;Data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token keyword"&gt;merge&lt;/SPAN&gt; have res1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;count1 &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;
		test_before &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'yes'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token keyword"&gt;else&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; count1 ne . and &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;count1 &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;
		test_after &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'yes'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I modified the count1 statement and this is what I need!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2020 21:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623710#M183662</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2020-02-10T21:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables using result of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623741#M183677</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id count result;
	cards;
1 1 0
1 2 0
1 3 1
1 4 0
1 5 0
1 6 1
2 1 0
2 2 0
3 1 1
4 1 0
4 2 0
4 3 1
5 1 1
5 2 0
5 3 1
;
data want;
 do n=1 by 1 until(last.id);
  set have;
  by id;
  if result then has_result=1;
  if result and not found then do;found=1;_n=n;end;
 end;
 
 do n=1 by 1 until(last.id);
  set have;
  by id;
  Test_Before='   ';Test_After='   ';
  if has_result then do;
   if n&amp;lt;_n then Test_Before='Yes';
   if n&amp;gt;_n then Test_After='Yes';
  end;
  output;
 end;
drop n found _n;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Feb 2020 04:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-using-result-of-an-existing-variable/m-p/623741#M183677</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-02-11T04:19:11Z</dc:date>
    </item>
  </channel>
</rss>

