<?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: Find column with NULL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797474#M296030</link>
    <description>&lt;P&gt;The problem with the WIDE structure is you cannot add a variable to a dataset once the data step has already started running. So you end up having to do some type of conditional code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use PROC TRANSPOSE to convert between the tall and wide layouts.&lt;/P&gt;
&lt;P&gt;Say you have this original dataset in WIDE format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wide ;
  length num 8 m1-m3 $10;
  input num m1-m3 ;
datalines;
12345 IN-18 IN-19 .
34565 IN-16 IN-24 IN-25
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you have this new data to append to it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  length num 8 m $10;
  input num m ;
datalines;
34565 IN-27
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first transpose the WIDE dataset, then interleave the two, and then re-transpose back to a new WIDE dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=wide
   out=tall(drop=_name_ rename=(col1=M) )
;
  by num;
  var m: ;
run;

data new_tall;
  set tall new ;
  by num;
  if missing(M) and not first.num then delete;
run;

proc transpose data=new_tall out=want(drop=_name_) prefix=M;
  by num;
  var m;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs     num      M1       M2       M3       M4

 1     12345    IN-18    IN-19
 2     34565    IN-16    IN-24    IN-25    IN-27
&lt;/PRE&gt;</description>
    <pubDate>Sun, 20 Feb 2022 16:19:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-20T16:19:39Z</dc:date>
    <item>
      <title>Find column with NULL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797432#M287528</link>
      <description>&lt;P&gt;I have dataset similar to the below&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA COMP_LIST;&lt;BR /&gt;input NUM M1 $ M2 $;&lt;BR /&gt;datalines ;&lt;BR /&gt;12345 IN .&lt;BR /&gt;34556 IN IN&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need to do based on a list provided check if that number exists in the above table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If it does, I need to check if there is a M* column that is NULL. If it is then update it with NULL.&lt;/P&gt;
&lt;P&gt;For example 12345 exists in the table above and M1 is not null and has value of IN. So as long the next M column exists update that with 'IN'. so M2 for 12345 has no value so update M2 with 'IN'&lt;/P&gt;
&lt;P&gt;2. if there is no M* column that is NULL then add the next M column to the table and update it with value.&lt;/P&gt;
&lt;P&gt;For example&amp;nbsp;34556 exists in the dataset and both M1 and M2 has values of 'IN' . so now add M3 to the dataset and update with 'IN'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this makes sense. What is the best way to achieve this ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 23:43:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797432#M287528</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2022-02-19T23:43:33Z</dc:date>
    </item>
    <item>
      <title>Re: Find column with NULL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797434#M287530</link>
      <description>&lt;P&gt;Not sure I understand what you are trying to do, but it looks to me like it would be a lot easier with a vertical structure instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input NUM @;
  length m $10;
  do index=1 by 1 until(missing(M));
    input m @;
    if not missing(m) then output;
  end;
datalines ;
12345 IN .
34556 IN IN
;

data want;
  set have;
  by num;
  output;
  if last.num then do;
    index+1;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs     NUM     m     index

 1     12345    IN      1
 2     12345    IN      2
 3     34556    IN      1
 4     34556    IN      2
 5     34556    IN      3

&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Feb 2022 00:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797434#M287530</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-20T00:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Find column with NULL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797446#M287540</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Think you got what I meant. definitely vertical structure would be easier. But what i need is a horizontal one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NUM&amp;nbsp; &amp;nbsp; &amp;nbsp;M1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M3&lt;/P&gt;
&lt;P&gt;12345&amp;nbsp; &amp;nbsp;IN-18&amp;nbsp; &amp;nbsp; IN-19&lt;/P&gt;
&lt;P&gt;34565&amp;nbsp; &amp;nbsp;IN-16&amp;nbsp; &amp;nbsp; IN-24&amp;nbsp; &amp;nbsp; IN-25&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to maintain an output as above. I would be adding a number to the IN. While I can check how many M columns are there by using the dictionary.columns table. What i am struggling to do is find if there is a M column with NULL value that I can update. If yes which one is that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in the example above, if I have a new entry for 34565, the should add a new column M4 to the dataset and populate it with say IN-28&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NUM&amp;nbsp; &amp;nbsp; &amp;nbsp;M1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M4&lt;/P&gt;
&lt;P&gt;12345&amp;nbsp; &amp;nbsp;IN-18&amp;nbsp; &amp;nbsp; IN-19&lt;/P&gt;
&lt;P&gt;34565&amp;nbsp; &amp;nbsp;IN-16&amp;nbsp; &amp;nbsp; IN-24&amp;nbsp; &amp;nbsp; IN-25&amp;nbsp; &amp;nbsp; IN-27&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 09:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797446#M287540</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2022-02-20T09:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find column with NULL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797474#M296030</link>
      <description>&lt;P&gt;The problem with the WIDE structure is you cannot add a variable to a dataset once the data step has already started running. So you end up having to do some type of conditional code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use PROC TRANSPOSE to convert between the tall and wide layouts.&lt;/P&gt;
&lt;P&gt;Say you have this original dataset in WIDE format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wide ;
  length num 8 m1-m3 $10;
  input num m1-m3 ;
datalines;
12345 IN-18 IN-19 .
34565 IN-16 IN-24 IN-25
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you have this new data to append to it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  length num 8 m $10;
  input num m ;
datalines;
34565 IN-27
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first transpose the WIDE dataset, then interleave the two, and then re-transpose back to a new WIDE dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=wide
   out=tall(drop=_name_ rename=(col1=M) )
;
  by num;
  var m: ;
run;

data new_tall;
  set tall new ;
  by num;
  if missing(M) and not first.num then delete;
run;

proc transpose data=new_tall out=want(drop=_name_) prefix=M;
  by num;
  var m;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs     num      M1       M2       M3       M4

 1     12345    IN-18    IN-19
 2     34565    IN-16    IN-24    IN-25    IN-27
&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Feb 2022 16:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797474#M296030</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-20T16:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Find column with NULL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797490#M313522</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Transpose seems to be the option then. Thank you very much for your time and solution&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 19:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-column-with-NULL/m-p/797490#M313522</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2022-02-20T19:21:38Z</dc:date>
    </item>
  </channel>
</rss>

