<?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: Keep the same order in when statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689179#M79637</link>
    <description>&lt;PRE&gt;proc sql ;
create table WANT as
select case when x&amp;lt;5 then "first"
when x&amp;lt;50 then "second"
when x&amp;lt;150 then "third"
else "last"
end as analysis,sum(n) as SUM
from HAVE
group by analysis;
quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;HAVE:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;n&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0,6&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;510&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Tue, 06 Oct 2020 13:11:36 GMT</pubDate>
    <dc:creator>mazouz</dc:creator>
    <dc:date>2020-10-06T13:11:36Z</dc:date>
    <item>
      <title>Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689167#M79631</link>
      <description>&lt;PRE&gt;PROC SQL;
CREATE TABLE WANT AS 
   SELECT 	
CASE 
WHEN A=0 THEN "FIRST"
WHEN B=0 THEN "SECOND"
WHEN C=0 THEN "THIRD"
ELSE "LAST" 
END AS ANALYSIS
FROM HAVE;&lt;BR /&gt;QUIT;&lt;/PRE&gt;&lt;P&gt;Hello Guys, I want to keep the same ordre in my "want" table&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;FIRST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SECONDE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;THIRD&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LAST&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the problem is in case statement SQL order by alphabetic like that:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;FIRST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LAST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SECONDE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;THIRD&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 06 Oct 2020 12:36:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689167#M79631</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-10-06T12:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689168#M79632</link>
      <description>&lt;P&gt;Yes, the default sort order of character variables is alphabetic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I don't understand, when you show the orders, this is an order in a SAS DATA set? The order then depends on the rows of the data set WANT and not the values of variable ANALYSIS. So its really not clear what you are asking for here. How do you get that final table?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 12:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689168#M79632</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T12:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689170#M79633</link>
      <description>&lt;P&gt;My question is if i can&amp;nbsp;&lt;SPAN&gt;modify the default sort order of character variables&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the result I want is :&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ANALYSIS&lt;/TD&gt;&lt;TD&gt;PERCENT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;FIRST&lt;/TD&gt;&lt;TD&gt;%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SECONDE&lt;/TD&gt;&lt;TD&gt;%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;THIRD&lt;/TD&gt;&lt;TD&gt;%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LAST&lt;/TD&gt;&lt;TD&gt;%&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;dont care about Percent variable it's just an exemple. but my object is to keep order Fisrt second third last not by alphabetic&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 12:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689170#M79633</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-10-06T12:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689171#M79634</link>
      <description>&lt;P&gt;How are you getting this table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it from a PROC? If so, be specific.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 12:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689171#M79634</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T12:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689178#M79636</link>
      <description>&lt;P&gt;I guess you see that "wrong" order when you later analyze your ANALYSIS variable, e.g. with PROC FREQ. Those outputs will be ordered alphabetically.&lt;/P&gt;
&lt;P&gt;If you create an underlying order through a format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value myvals
  1 = 'First'
  2 = 'Second'
  3 = 'Third'
  99 = 'Last'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and change the WHEN clause to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case
  when A = 0 then 1
  when B = 0 then 2"
  when C = 0 then 3
  else 99 
end as analysis format=myvals.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you can use options like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have order=internal;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;later to keep the wanted order in the output.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689178#M79636</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-06T13:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689179#M79637</link>
      <description>&lt;PRE&gt;proc sql ;
create table WANT as
select case when x&amp;lt;5 then "first"
when x&amp;lt;50 then "second"
when x&amp;lt;150 then "third"
else "last"
end as analysis,sum(n) as SUM
from HAVE
group by analysis;
quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;HAVE:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;n&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0,6&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;510&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689179#M79637</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-10-06T13:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689187#M79640</link>
      <description>&lt;P&gt;Now that's pretty obvious:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/339736"&gt;@mazouz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;proc sql ;
create table WANT as
select case when x&amp;lt;5 then "first"
when x&amp;lt;50 then "second"
when x&amp;lt;150 then "third"
else "last"
end as analysis,sum(n) as SUM
from HAVE
group by analysis;
quit;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You create a character variable and use it in a GROUP BY, and that means alphabetic sorting.&lt;/P&gt;
&lt;P&gt;By using the format as I suggested in my previous post, the order in the output will be as you want it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x n;
datalines;
0.6 1
1 2
10  70
25  120
60  60
500 3
510 1
;

proc format;
value myvals
  1 = 'First'
  2 = 'Second'
  3 = 'Third'
  99 = 'Last'
;
run;

proc sql;
create table WANT as
  select
    case
      when x&amp;lt;5 then 1
      when x&amp;lt;50 then 2
      when x&amp;lt;150 then 3
      else 99
    end as analysis format=myvals.,
    sum(n) as SUM
  from HAVE
  group by analysis
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689187#M79640</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-06T13:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the same order in when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689521#M79641</link>
      <description>Or could padding some white blanks before it. and order by it .&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE WANT AS &lt;BR /&gt;   SELECT 	&lt;BR /&gt;CASE &lt;BR /&gt;WHEN A=0 THEN "    FIRST"&lt;BR /&gt;WHEN B=0 THEN "   SECOND"&lt;BR /&gt;WHEN C=0 THEN "  THIRD"&lt;BR /&gt;ELSE "LAST" &lt;BR /&gt;END AS ANALYSIS  length=20&lt;BR /&gt;FROM HAVE&lt;BR /&gt;order by analysis ;&lt;BR /&gt;QUIT;</description>
      <pubDate>Wed, 07 Oct 2020 12:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-the-same-order-in-when-statement/m-p/689521#M79641</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-07T12:28:21Z</dc:date>
    </item>
  </channel>
</rss>

