<?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: Sort missing character variable values so that missing values appear last in dataset in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697444#M25403</link>
    <description>&lt;P&gt;Hi, yes it is a requirement to sort SSN ascending. Thank you for clarifying.&lt;/P&gt;</description>
    <pubDate>Sun, 08 Nov 2020 16:38:50 GMT</pubDate>
    <dc:creator>mlensing</dc:creator>
    <dc:date>2020-11-08T16:38:50Z</dc:date>
    <item>
      <title>Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697405#M25399</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I'm still a bit new to SAS, I wanted to reach out for guidance surrounding sorting my dataset. I have created a master dataset composed of 3 datasets in which 2 have filled values for SSN (character variable) while the last does not have any values for SSN. I want to sort my master dataset by SSN and so that the missing values appear last/at the end of the dataset. Is this possible and is there a straightforward way to do this? Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;DATA HypTabs.Contact;&lt;/DIV&gt;&lt;DIV&gt;LENGTHSSN $11.&lt;/DIV&gt;&lt;DIV&gt;Inits $3.&lt;/DIV&gt;&lt;DIV&gt;City $20.&lt;/DIV&gt;&lt;DIV&gt;StateCd $2.&lt;/DIV&gt;&lt;DIV&gt;ZipCd $5.;&lt;/DIV&gt;&lt;DIV&gt;SETWORK.Contact_IA&lt;/DIV&gt;&lt;DIV&gt;WORK.Contact_MS&lt;/DIV&gt;&lt;DIV&gt;WORK.Contact_UT;&lt;/DIV&gt;&lt;DIV&gt;LABELSSN= 'Social Security Number'&lt;/DIV&gt;&lt;DIV&gt;Inits= 'Subject Initials'&lt;/DIV&gt;&lt;DIV&gt;City= 'City'&lt;/DIV&gt;&lt;DIV&gt;StateCd= 'State Code'&lt;/DIV&gt;&lt;DIV&gt;ZipCd= 'Zip Code';&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;PROC SORT DATA = HypTabs.Contact;&lt;/DIV&gt;&lt;DIV&gt;BY SSN;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;(Note: When I do PROC SORT by SSN, observations 1-195 are blank/missing which corresponds to the dataset in which those values are missing.)&lt;/DIV&gt;</description>
      <pubDate>Sun, 08 Nov 2020 06:19:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697405#M25399</guid>
      <dc:creator>mlensing</dc:creator>
      <dc:date>2020-11-08T06:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697410#M25400</link>
      <description>&lt;P&gt;You could simply sort by Descending SSN.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA = HypTabs.Contact;
BY descending SSN;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it a requirement that besides missing data, the SSN's are sorted ascending?&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 07:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697410#M25400</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-08T07:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697421#M25401</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309587"&gt;@mlensing&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are various ways to achieve what you want. &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304" target="_blank" rel="noopener"&gt;draycut&lt;/A&gt;'s suggestion is short and elegant. To sort the non-missing SSN values first in &lt;EM&gt;ascending&lt;/EM&gt; order, followed by the missing values, you could create an additional sort key in your DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;...&lt;/FONT&gt;
&lt;FONT color="#999999"&gt;set work.Contact_IA
    work.Contact_MS
    work.Contact_UT&lt;/FONT&gt;&lt;STRONG&gt;(in=UT)&lt;/STRONG&gt;;
&lt;STRONG&gt;nossn=UT;&lt;/STRONG&gt;
&lt;FONT color="#999999"&gt;...&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=ledsoptsref&amp;amp;docsetTarget=n1p1o2dsuc465nn198ovwdrj9mvy.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;IN= dataset option&lt;/A&gt; creates a temporary 0-1 flag so that &lt;FONT face="courier new,courier"&gt;UT=1&lt;/FONT&gt; characterizes observations coming from &lt;FONT face="courier new,courier"&gt;work.Contact_UT&lt;/FONT&gt;&amp;nbsp;(assuming that these are the records with missing SSN). The subsequent assignment statement makes this flag permanent, now named &lt;FONT face="courier new,courier"&gt;nossn&lt;/FONT&gt;. Adding variable &lt;FONT face="courier new,courier"&gt;nossn&lt;/FONT&gt; as the first sort key in the BY statement of your PROC SORT step ensures that observations with &lt;FONT face="courier new,courier"&gt;nossn=0&lt;/FONT&gt;, i.e., the observations from &lt;FONT face="courier new,courier"&gt;Contact_IA&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;Contact_MS&lt;/FONT&gt;, are sorted first, followed by those with &lt;FONT face="courier new,courier"&gt;nossn=1&lt;/FONT&gt; from &lt;FONT face="courier new,courier"&gt;Contact_UT&lt;/FONT&gt;. You may want to drop variable &lt;FONT face="courier new,courier"&gt;nossn&lt;/FONT&gt; from the final dataset (commented out below):&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;proc sort data=HypTabs.Contact&lt;/FONT&gt; /* out=HypTabs.Contact(drop=nossn) */&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;
&lt;FONT color="#999999"&gt;by&lt;/FONT&gt; &lt;STRONG&gt;nossn&lt;/STRONG&gt;&lt;FONT color="#999999"&gt; ssn;&lt;/FONT&gt;
&lt;FONT color="#999999"&gt;run;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you can take advantage of the flexibility of an &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=sqlproc&amp;amp;docsetTarget=n0gwogdxntzooun1azrzwrwrqvzq.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;ORDER BY clause&lt;/A&gt; in PROC SQL. There you can create an additional sort key "on the fly," i.e., you don't need to modify your DATA step:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as
select * from HypTabs.Contact;
order by &lt;STRONG&gt;missing(ssn)&lt;/STRONG&gt;, ssn;
quit;&lt;/PRE&gt;
&lt;P&gt;Observations with missing SSN have &lt;FONT face="courier new,courier"&gt;missing(ssn)=1&lt;/FONT&gt;, otherwise &lt;FONT face="courier new,courier"&gt;missing(ssn)=0&lt;/FONT&gt;. The sort order within these two subsets is not guaranteed by PROC SQL, though, so you may want to add more sort keys to define it.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 11:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697421#M25401</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-11-08T11:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697422#M25402</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort
  data=HypTabs.Contact (
    where=(SSN ne "")
  )
  out=want
;
by SSN;
run;

proc append
  base=want
  data=HypTabs.Contact (
    where=(SSN = "")
  )
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Nov 2020 11:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697422#M25402</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-08T11:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697444#M25403</link>
      <description>&lt;P&gt;Hi, yes it is a requirement to sort SSN ascending. Thank you for clarifying.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 16:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697444#M25403</guid>
      <dc:creator>mlensing</dc:creator>
      <dc:date>2020-11-08T16:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697446#M25404</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309587"&gt;@mlensing&lt;/a&gt;,&amp;nbsp;then&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;s answer is the way to go &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 16:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697446#M25404</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-08T16:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: Sort missing character variable values so that missing values appear last in dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697496#M25415</link>
      <description>Thank you so much, this worked perfectly! I really appreciate your help and thorough response!</description>
      <pubDate>Mon, 09 Nov 2020 03:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Sort-missing-character-variable-values-so-that-missing-values/m-p/697496#M25415</guid>
      <dc:creator>mlensing</dc:creator>
      <dc:date>2020-11-09T03:21:58Z</dc:date>
    </item>
  </channel>
</rss>

