<?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: How to conditionally enter new records in a SAS dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671524#M201672</link>
    <description>Hi smantha,&lt;BR /&gt;&lt;BR /&gt;Thanks for your help. However, the variables Test, Status and Frq are not fixed at 3 levels.I put them at 3 to construct an example input data. But I can take the maximum level from Test, put that in a macro variable and use the code.&lt;BR /&gt;Thanks again.</description>
    <pubDate>Wed, 22 Jul 2020 18:21:44 GMT</pubDate>
    <dc:creator>kingCobra</dc:creator>
    <dc:date>2020-07-22T18:21:44Z</dc:date>
    <item>
      <title>How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671452#M201641</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a tricky programming question.&lt;/P&gt;
&lt;P&gt;How do I add new records based on the value of a variable in the subsequent record?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example, I have this input data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Subj&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Test&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Status&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; Frq&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Missed previous&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Missed previous&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the following output data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Subj&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Test&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Status&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; Frq&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;1002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Test 2&amp;nbsp; &amp;nbsp; &amp;nbsp; Missed&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;1003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Test 1&amp;nbsp; &amp;nbsp; &amp;nbsp; Missed&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;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;1003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Missed&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;1003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Test 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Done&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;1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the records in red are added based on the value in Status column.&lt;/P&gt;
&lt;P&gt;Can anyone help me create a dynamic SAS code to get the output dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 16:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671452#M201641</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-07-22T16:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671473#M201654</link>
      <description>&lt;P&gt;Here's one approach. Whenever a status of "Missed previous" is encountered, output records appropriately to fill in those missing tests. Then sort the data at the end.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input subj $ test &amp;amp; $ status &amp;amp; $20. frq;
    datalines;
    1001   Test 1   Done             1
    1001   Test 2   Done             1
    1001   Test 3   Done             1
    1002   Test 1   Done             1
    1002   Test 3   Missed previous  1
    1003   Test 3   Missed previous  2
    ;
run;

data have_2 (drop=i freq);
    set have (rename=(frq=freq));
    if status = "Missed previous" then do;
        status = "Done";
        frq = 1;
        output;
        do i = 1 to freq;
            test = "Test " || put(input(char(test, 6), 1.) -1, 1.);
            status = "Missed";
            frq = .;
            output;
        end;
    end;
    else do;
        frq = freq;
        output;
    end;
run;

proc sort data=have_2 out=want; 
    by subj test;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 17:14:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671473#M201654</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-07-22T17:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671490#M201660</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
infile datalines dlm=',';
informat id 4. test $12. Status $32. freq 3.;
input id Test $ Status $ freq;
datalines;
1001,Test 1,Done,1
1001,Test 2,Done,1
1001,Test 3,Done,1
1002,Test 1,Done,1
1002,Test 3,Missed previous,1
1003,Test 3,Missed previous,2
;;;
run;
proc sort; By id test;
run;

data want;
set new;
array _test{3} $12. ;
array _status{3} $12. ;
array _frq{3} ;
by id Test;
retain  _test: _status: ' ' count _frq: 0;
if first.id then do;
 call missing(of _test1 - _test3);
 call missing(of _frq1 - _frq3);
 call missing(of _status1 - _status3);
 count=0;
end;
count=input(scan(test,-1),??4.);

	_test[count] = test;
	_status[count] = status;
	_frq[count] = freq;

if last.id then do;
  do i = 1 to 3;
  	if missing(_test[i]) then test = "Test "||put(i,??$1.);
	   else test = _test[i];
    if missing(_status[i]) then status = "Missed";
	   else Status = "Done";
	freq=_frq[i];
    *put _all_;
	output;
  end;
end;
drop _test: _status: _frq: i count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 17:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671490#M201660</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-22T17:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671518#M201669</link>
      <description>Thanks for the help.</description>
      <pubDate>Wed, 22 Jul 2020 18:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671518#M201669</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-07-22T18:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671524#M201672</link>
      <description>Hi smantha,&lt;BR /&gt;&lt;BR /&gt;Thanks for your help. However, the variables Test, Status and Frq are not fixed at 3 levels.I put them at 3 to construct an example input data. But I can take the maximum level from Test, put that in a macro variable and use the code.&lt;BR /&gt;Thanks again.</description>
      <pubDate>Wed, 22 Jul 2020 18:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671524#M201672</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-07-22T18:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671609#M201696</link>
      <description>&lt;P&gt;It is possible for all tests for a given subject are missed? Suppose a subject 1004 had Test 1 and Test 2 (both missed). It may be adventitious to have a separate list of given tests and then compare if the original list has matching records in this bank list of all tests given by subject. I use SQL all day so I found it easier to use PROC SQL for this.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input subj $ test &amp;amp; $ status &amp;amp; $20. frq;
    datalines;
    1001   Test 1   Done             1
    1001   Test 2   Done             1
    1001   Test 3   Done             1
    1002   Test 1   Done             1
    1002   Test 3   Missed previous  1
    1003   Test 3   Missed previous  2
    ;
run;

data bank;
	input subj $ test &amp;amp; $;
	datalines;
    1001   Test 1   
    1001   Test 2   
    1001   Test 3   
    1002   Test 1   
    1002   Test 2   
    1002   Test 3   
    1003   Test 1   
    1003   Test 2   
    1003   Test 3   
    1004   Test 1   
    1004   Test 2   
	;
run;

PROC SQL;
	Create Table temp AS
	SELECT
		tbl1.subj AS Subject
		, tbl1.test AS Test
		, CASE WHEN MISSING(Tbl2.subj) THEN 'Missed' ELSE 'Done' END AS Status
	FROM work.bank as tbl1
	LEFT JOIN work.have as tbl2
		ON tbl1.subj = tbl2.subj
		AND tbl1.test = tbl2.test
	;
QUIT;		
PROC SORT DATA = temp Out = Temp2;
	BY Subject Test;
RUN;
PROC PRINT DATA = Temp2; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 20:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671609#M201696</guid>
      <dc:creator>BrennanBux</dc:creator>
      <dc:date>2020-07-22T20:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally enter new records in a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671741#M201751</link>
      <description>Yes it is possible to construct an exhaustive list of all Tests available. I'll try your code (the Proc SQL way) .</description>
      <pubDate>Thu, 23 Jul 2020 10:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-enter-new-records-in-a-SAS-dataset/m-p/671741#M201751</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-07-23T10:22:36Z</dc:date>
    </item>
  </channel>
</rss>

