<?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 How to simplify long case when coding with loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562482#M157569</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm in a situation where I have to write many "case when" statements to create each of the many new variables in proc sql, while those "case when" statements are of a highly recognized pattern: when the variable starts with A then "A000", when it starts with B then "B000", etc. and it goes all the way to "Z000". I would have to write 26 "case when" statements to create one new variable. I think there must be a way to simplify and shorten the codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my original codes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

select var1, var2

&amp;nbsp; , case when substr(upper(var1),1,1) = 'A' then 'A000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'B' then 'B000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'C' then 'C000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'D' then 'D000'

...

...

...

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'Z' then 'Z000'

&amp;nbsp; &amp;nbsp;end as var1_new

from table;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope there's a way to save me from this exhausting coding. Any help would be very much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 May 2019 07:00:13 GMT</pubDate>
    <dc:creator>elisazhsn</dc:creator>
    <dc:date>2019-05-30T07:00:13Z</dc:date>
    <item>
      <title>How to simplify long case when coding with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562482#M157569</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm in a situation where I have to write many "case when" statements to create each of the many new variables in proc sql, while those "case when" statements are of a highly recognized pattern: when the variable starts with A then "A000", when it starts with B then "B000", etc. and it goes all the way to "Z000". I would have to write 26 "case when" statements to create one new variable. I think there must be a way to simplify and shorten the codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my original codes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

select var1, var2

&amp;nbsp; , case when substr(upper(var1),1,1) = 'A' then 'A000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'B' then 'B000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'C' then 'C000'

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'D' then 'D000'

...

...

...

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when substr(upper(var1),1,1) = 'Z' then 'Z000'

&amp;nbsp; &amp;nbsp;end as var1_new

from table;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope there's a way to save me from this exhausting coding. Any help would be very much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 07:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562482#M157569</guid>
      <dc:creator>elisazhsn</dc:creator>
      <dc:date>2019-05-30T07:00:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify long case when coding with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562484#M157570</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not tested but something like below should cover all letters at once.&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; case when findc(substr(var1,1,1),,'LU')=1&amp;nbsp;then&amp;nbsp;substr(upcase(var1),1,1)||'000'
&amp;nbsp;else&amp;nbsp;&amp;lt;?&amp;gt;
&amp;nbsp;end&amp;nbsp;as&amp;nbsp;var1_new&amp;nbsp;length=$&amp;lt;?&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n1mdh2gvd5potjn14jipysvzn4o7.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n1mdh2gvd5potjn14jipysvzn4o7.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 07:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562484#M157570</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-30T07:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify long case when coding with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562491#M157575</link>
      <description>&lt;P&gt;Thank you a lot for your quick reply!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't get 'LU' in your findc() function, what does it mean? Also, does &amp;lt;?&amp;gt; mean fuzzy search here?&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 08:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562491#M157575</guid>
      <dc:creator>elisazhsn</dc:creator>
      <dc:date>2019-05-30T08:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify long case when coding with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562493#M157577</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/276107"&gt;@elisazhsn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you a lot for your quick reply!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't get 'LU' in your findc() function, what does it mean? Also, does &amp;lt;?&amp;gt; mean fuzzy search here?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;'LU' stand for Lowercase letters and Uppercase letters. The link I've posted will take you to the docu page for findc() where you can get the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;?&amp;gt; means that this is stuff you have replace with what's suitable for your case. I don't know what you want to do in the ELSE case and I also can't know how long the resulting variable needs to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 08:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-long-case-when-coding-with-loop/m-p/562493#M157577</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-30T08:31:13Z</dc:date>
    </item>
  </channel>
</rss>

