<?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: Loop through several similar columns in dataset, output only if populated, and stack data vertic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475864#M286032</link>
    <description>&lt;P&gt;Mildly quick and easy would be to use arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array comm {3} comment1-comment3;&lt;/P&gt;
&lt;P&gt;do k=1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if comm{k} &amp;gt; ' ' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; comment = vname(comm{k});&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; FieldValue = comm{k};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep comment PID FieldValue;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jul 2018 23:03:03 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-07-05T23:03:03Z</dc:date>
    <item>
      <title>Loop through several similar columns in dataset, output only if populated, and stack data vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475823#M286031</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with multiple “comments” fields which largely remain empty. I want to create a QA dataset which basically dumps each of these fields when populated, and reformats the data vertically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID, comment1, comment2, comment3&lt;/P&gt;&lt;P&gt;111,, ‘Not completed on this date.’&lt;/P&gt;&lt;P&gt;112,,,&lt;/P&gt;&lt;P&gt;113, ‘Comment comment’, ‘LTFU’,&lt;/P&gt;&lt;P&gt;114, ‘LTFU,, ‘No result as of 1/1/2018.’&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a dataset which looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Comment,PID,FieldValue&lt;/P&gt;&lt;P&gt;Comment1, 113, ‘Comment comment’&lt;/P&gt;&lt;P&gt;Comment1, 114, ‘LTFU’&lt;/P&gt;&lt;P&gt;Comment2, 113,&amp;nbsp;‘LTFU’&lt;/P&gt;&lt;P&gt;Comment3, 111, ‘Not completed on this date.’&lt;/P&gt;&lt;P&gt;Comment3, 114, ‘No result as of 1/1/2018.’&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As more of an sql-programmer, my back-up way to do this would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql feedback;&lt;/P&gt;&lt;P&gt;create table want as&lt;/P&gt;&lt;P&gt;select&lt;/P&gt;&lt;P&gt;&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; ‘comment1’ as Comment,&lt;/P&gt;&lt;P&gt;&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; PID,&lt;/P&gt;&lt;P&gt;&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; Comment1 as fieldvalue&lt;/P&gt;&lt;P&gt;from work.have&lt;/P&gt;&lt;P&gt;where comment1&amp;lt;&amp;gt;’’&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UNION ALL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select&lt;/P&gt;&lt;P&gt;&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; ‘comment2’ as Comment,&lt;/P&gt;&lt;P&gt;&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; PID,&lt;/P&gt;&lt;P&gt;&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; Comment2 as fieldvalue&lt;/P&gt;&lt;P&gt;from work.have&lt;/P&gt;&lt;P&gt;where comment2&amp;lt;&amp;gt;’’&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;…etc…&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But there must be a more efficient (and no doubt blindingly basic) way to do this...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your input/patience.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 20:49:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475823#M286031</guid>
      <dc:creator>hawkmoon</dc:creator>
      <dc:date>2018-07-05T20:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through several similar columns in dataset, output only if populated, and stack data vertic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475864#M286032</link>
      <description>&lt;P&gt;Mildly quick and easy would be to use arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array comm {3} comment1-comment3;&lt;/P&gt;
&lt;P&gt;do k=1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if comm{k} &amp;gt; ' ' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; comment = vname(comm{k});&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; FieldValue = comm{k};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep comment PID FieldValue;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 23:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475864#M286032</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-05T23:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through several similar columns in dataset, output only if populated, and stack data vertic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475865#M286033</link>
      <description>&lt;P&gt;Use proc transpose.&amp;nbsp; Add a WHERE= condition on the output dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have 
  out=want (rename=(col1=comment) where=(not missing(comment)))
;
  by pid ;
  var comment1-comment3 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jul 2018 23:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475865#M286033</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-05T23:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through several similar columns in dataset, output only if populated, and stack data vertic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475867#M286034</link>
      <description>&lt;P&gt;Thank you both for your very quick and much more palatable solutions!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Quick follow-up question: Do my variables have to have numeric suffixes for these solutions to work? I.e., I used varnames "Comment1, Comment2, etc." as an example, but really they have names like "medication_spec", "ethnicity_spec", etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again! I can already see multiple used for both these sets of code in my work.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 23:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475867#M286034</guid>
      <dc:creator>hawkmoon</dc:creator>
      <dc:date>2018-07-05T23:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through several similar columns in dataset, output only if populated, and stack data vertic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475875#M286035</link>
      <description>&lt;P&gt;No.&lt;/P&gt;
&lt;P&gt;You can use any list of variables in an ARRAY or VAR statement.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jul 2018 00:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-several-similar-columns-in-dataset-output-only-if/m-p/475875#M286035</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-06T00:44:00Z</dc:date>
    </item>
  </channel>
</rss>

