<?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: Help with code approach in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665695#M199088</link>
    <description>&lt;P&gt;Please choose a solution.&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jun 2020 05:15:13 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-06-29T05:15:13Z</dc:date>
    <item>
      <title>Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665516#M199008</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below case where I'm unable to find a best approach to implement the solution, could you please advise me here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input data: (in the below example I have the item and year columns grouped, and based on the values of X1 and X2 , valid and error datasets are created.&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Item&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;X1&lt;/TD&gt;&lt;TD&gt;X2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B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&gt;&lt;LI&gt;Valid dataset will always have a unique entry from the group. The non-null values of X1 and X2 in a group is considered to be valid(ex - Item : A)&lt;/LI&gt;&lt;LI&gt;when X1/X2 have same values in a group, then the unique value is picked and added to the valid dataset (ex- item :C)&lt;/LI&gt;&lt;LI&gt;Conflicts in values of X1/X2 in a group are considered to be an error. when X1/X2 values are different in a group then the last value of X1/X2 in the group are picked as a valid. So in this case the valid dataset will have a unique entry for the mentioned group (ex- Item: B ) and the error dataset will save the both the records of B from the input dataset.&lt;/LI&gt;&lt;LI&gt;When there is only in entry in the input, then it goes to the valid dataset (ex - Item: D)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output1 - Valid dataset:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Item&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;X1&lt;/TD&gt;&lt;TD&gt;X2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;E&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output2 : Error dataset&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Item&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;X1&lt;/TD&gt;&lt;TD&gt;X2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;E&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;E&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jun 2020 10:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665516#M199008</guid>
      <dc:creator>Kirtid</dc:creator>
      <dc:date>2020-06-27T10:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665520#M199009</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/238772"&gt;@Kirtid&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines dsd missover dlm="09"x;
	input Item $ Year X1 X2;
	datalines;
A	2018	 	12
A	2018	10	 
B	2019	 	20
B	2019	 	5
C	2017	1	 
C	2017	1	2
C	2017	 	 
D	2016	 	 
E	2016	10	2
E	2016	6	 
;

/* Identify record number within each group item-year */
data have2;
	set have;
	by Item Year;
	if first.item then num=0;
	num+1;
run;

/* Create OUTPUT1 */

data output1;
	set have2;
	by item year num;
	retain x1_bis x2_bis;
	if first.year then call missing(x1_bis, x2_bis);
	if not missing(x1) then x1_bis=x1;
	if not missing(x2) then x2_bis=x2;
	if last.year then output;
	drop x1 x2;
	rename x1_bis=x1 x2_bis=x2;
run;

/* Identify conflicts in values of X1/X2 -&amp;gt; OUTPUT2 */
proc sql;
	create table output2 as
	
	select *
	from have2
	where x1 is not missing
	group by item, year
	having count(distinct x1) &amp;gt; 1
	
	union all corr
	
	select *
	from have2
	where x2 is not missing
	group by item, year
	having count(distinct x2) &amp;gt; 1
	
	order by item, year, num;
quit;

proc print data=output1;run;
proc print data=output2;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jun 2020 10:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665520#M199009</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-27T10:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665532#M199013</link>
      <description>&lt;P&gt;This is untested but ought to work.&amp;nbsp; GIven that your data is sorted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
   by item year;
   var a b;
   output out=validity (keep=item year min_a min_b max_a max_b)
   min=min_a min_b max=max_a max_b;
run;
data valid invalid;
   merge have validity;
   by item year;
   if (min_a &amp;lt; max_a) or (min_b &amp;lt; max_b) then output invalid;
   else output valid;
   drop min_a min_b max_a max_b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the handling of missing values is tricky but accurate.&amp;nbsp; PROC SUMMARY ignores missing values when calculating statistics.&amp;nbsp; The only way MIN_A or MIN_B ends up with a missing value is when all the values of the incoming variable are missing for that group of observations.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jun 2020 13:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665532#M199013</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-06-27T13:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665535#M199016</link>
      <description>&lt;P&gt;Here is a relatively simple data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data valid error_keys(keep=Item Year);
  merge 
    have(keep=Item Year)
    have(keep=Item Year X1 where=(X1 is not null) in=in1)
    have(keep=Item Year X2 where=(X2 is not null) in=in2)
    ;
  by item year;
  if first.year then do;
    _x1=x1;
    _x2=x2;
    _error=0;
    end;
  else do;
    if in1 and _x1 ne x1 then do;
      _error=1;
      _x1=x1;
      end;
    if in2 and _x2 ne x2 then do;
      _error=1;
      _x2=x2;
      end;
    end;
  if last.year;
  x1=_x1;
  x2=_x2;
  output valid;
  if _error then output error_keys;
  retain _:;
  drop _:;
run;

data errors;
  merge error_keys(in=error) have;
  by item year;
  if error;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jun 2020 13:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665535#M199016</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-27T13:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665631#M199068</link>
      <description>&lt;P&gt;Thank you all much for your time and for giving me a different view on the solution approach, it's really interesting for me to note them. Have a great day..&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jun 2020 11:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665631#M199068</guid>
      <dc:creator>Kirtid</dc:creator>
      <dc:date>2020-06-28T11:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665632#M199069</link>
      <description>&lt;P&gt;Thanks for the solution. I have a question here, the solution worked for the provided example, but in original I have around 10 columns(X1... X10) for which this valid check to be performed, in such case using 'union corr all' would it be fine while creating the invalid dataset?, wondering if there will be any performance issues, kindly advise.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jun 2020 11:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665632#M199069</guid>
      <dc:creator>Kirtid</dc:creator>
      <dc:date>2020-06-28T11:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help with code approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665695#M199088</link>
      <description>&lt;P&gt;Please choose a solution.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 05:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-code-approach/m-p/665695#M199088</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-06-29T05:15:13Z</dc:date>
    </item>
  </channel>
</rss>

