<?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: general problem / solution in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325929#M271485</link>
    <description>&lt;P&gt;It depends what you mean by "first". &amp;nbsp;If you mean the first variable using the internal order in which SAS is storing the variables, you can use:&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 charvars&amp;nbsp;{*} _character_;&lt;/P&gt;
&lt;P&gt;do firstone=1 to dim(charvars) until (charvars{firstone} &amp;gt; ' ');&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if firstone&amp;nbsp;&amp;gt; dim(charvars) then firstone=0; &amp;nbsp; /* just in case there are no missing values */&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800000"&gt;else name_of_first = vname(charvars{firstone});&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have your own "order" in mind for searching, you will need to replace _CHARACTER_ with a list of variable names in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800000"&gt;Added a statement in case you are looking to get the name of the first variable.&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2017 11:40:54 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-01-19T11:40:54Z</dc:date>
    <item>
      <title>general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325927#M271484</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem that i think has a general solution. It is like this&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;Problem basis:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset have observations with unknown number of variables&lt;BR /&gt;all variables are of type text&lt;BR /&gt;some of them are empty and which ones are varies from one observation to another&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for all observations, ignore all empty variables and return only the value of first occurrence of variable with text per observation&lt;/P&gt;&lt;P&gt;------------------------------------------------------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;New to SAS, I can figure this out with number variables but ran into a dead-end with text-variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Anyone here that see an obvious solution? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 10:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325927#M271484</guid>
      <dc:creator>Gen-E79</dc:creator>
      <dc:date>2017-01-19T10:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325929#M271485</link>
      <description>&lt;P&gt;It depends what you mean by "first". &amp;nbsp;If you mean the first variable using the internal order in which SAS is storing the variables, you can use:&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 charvars&amp;nbsp;{*} _character_;&lt;/P&gt;
&lt;P&gt;do firstone=1 to dim(charvars) until (charvars{firstone} &amp;gt; ' ');&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if firstone&amp;nbsp;&amp;gt; dim(charvars) then firstone=0; &amp;nbsp; /* just in case there are no missing values */&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800000"&gt;else name_of_first = vname(charvars{firstone});&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have your own "order" in mind for searching, you will need to replace _CHARACTER_ with a list of variable names in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800000"&gt;Added a statement in case you are looking to get the name of the first variable.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 11:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325929#M271485</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T11:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325932#M271486</link>
      <description>&lt;P&gt;As an alternative to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s great example:&lt;/P&gt;
&lt;PRE&gt;data want;
  infile datalines dlm="," dsd;
  input var1 $ varb $ xyz $;
  first_result=scan(catx(",",of _character_),1,",");
datalines;
astring,something,yv
,nomiss,
,,abc
;
run;&lt;/PRE&gt;
&lt;P&gt;The catx() creates a string with your character variables where present seprated by , so the first of that string is going to be the first non-missing.&lt;/P&gt;
&lt;P&gt;Do note that is is a good idea to post some test data, in the form of a datastep, so we can provide code working for your data.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 11:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325932#M271486</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-19T11:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325957#M271487</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp; the coalesceC function seems the obvious choice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   infile cards dsd missover;
   input (var1 varb xyz) ($);
   First_not_blank = coalesceC(of var1-character-xyz);
   cards;
astring,something,yv

,nomiss
,,abc
;;;;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG title="Capture.PNG" alt="Capture.PNG" src="https://communities.sas.com/t5/image/serverpage/image-id/6797i87C687A84004D37B/image-size/original?v=1.0&amp;amp;px=-1" border="0" /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 13:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325957#M271487</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-01-19T13:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325978#M271488</link>
      <description>&lt;P&gt;You could easily be right about what's needed.&amp;nbsp; In that case, you don't really need to know the variable names:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First_not_blank = coalescec(of _character_);&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 14:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325978#M271488</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T14:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325990#M271489</link>
      <description>&lt;P&gt;Or use the shortened:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First_not_blank=coalescec(of _char_);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any advances&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 15:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/325990#M271489</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-19T15:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/326014#M271490</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Or use the shortened:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First_not_blank=coalescec(of _char_);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any advances&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, while not an issue for&amp;nbsp;your example,&amp;nbsp;that syntax will included the variable on the left in the list _CHAR_.&amp;nbsp; I would normally define&amp;nbsp;an ARRAY just after SET statement to have the benifit of using _CHAR_ and not including unwanted variables in the coalesceC list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set ...;
array _c[*] _character_;
first_no_blank = coalesce(of _c[*]);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2017 15:33:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/326014#M271490</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-01-19T15:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: general problem / solution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/326048#M271491</link>
      <description>&lt;P&gt;Appreciate the help and the quick response time guys, thats just straight up impressive! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 16:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/general-problem-solution/m-p/326048#M271491</guid>
      <dc:creator>Gen-E79</dc:creator>
      <dc:date>2017-01-19T16:52:53Z</dc:date>
    </item>
  </channel>
</rss>

