<?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: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776679#M247050</link>
    <description>yeah. I tried order by, but there are new problem caused. The result looks like a_1 ... z_1, a_10 ... z_10, a_100 ... z_100, a_101 ... z_101</description>
    <pubDate>Wed, 27 Oct 2021 06:52:40 GMT</pubDate>
    <dc:creator>ttqkroe</dc:creator>
    <dc:date>2021-10-27T06:52:40Z</dc:date>
    <item>
      <title>Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776667#M247044</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a huge dataset and i have different variables with prefix _1 to _108, and I want to reorder them by the prefix. For example, the order should be a_1, b_1 ... z_1 then a_2, b_2 ..., z_2. I used code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Select name into :var_1 separated by ","&lt;/P&gt;&lt;P&gt;from x&lt;/P&gt;&lt;P&gt;where name like '%_1';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;create table xxx as&lt;/P&gt;&lt;P&gt;select ID&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;,&amp;amp;var_1.&lt;/P&gt;&lt;P&gt;from xx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just those codes 108 times because I do not know how to use loop to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I got a result only the prefix with _10, _20, _30 ...., _90 has the correct order that I want.&amp;nbsp; The log shows that the rest variables already existsz on file work.xxx.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please Help, THANKS!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 05:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776667#M247044</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2021-10-27T05:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776671#M247045</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select trim(NAME) into :vars separated by ','
from HAVE
where NAME ne 'ID'
sorted by scan(NAME,2,'_'), NAME;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table WANT as
select ID, &amp;amp;vars from HAVE;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 06:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776671#M247045</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-27T06:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776673#M247046</link>
      <description>No, it does not work. There is a syntax error on "sorted by scan(Name,2,'_'), name"</description>
      <pubDate>Wed, 27 Oct 2021 06:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776673#M247046</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2021-10-27T06:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776677#M247048</link>
      <description>&lt;P&gt;&lt;STRONG&gt;order by&lt;/STRONG&gt; not &lt;STRONG&gt;sorted by&lt;/STRONG&gt;. Sorry.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 06:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776677#M247048</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-27T06:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776679#M247050</link>
      <description>yeah. I tried order by, but there are new problem caused. The result looks like a_1 ... z_1, a_10 ... z_10, a_100 ... z_100, a_101 ... z_101</description>
      <pubDate>Wed, 27 Oct 2021 06:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776679#M247050</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2021-10-27T06:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776680#M247051</link>
      <description>I just realized that it cannot check the prefix as a whole thing. For 102, it will be order as 1 then 0 then 2 instead of 102 as a whole.</description>
      <pubDate>Wed, 27 Oct 2021 06:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776680#M247051</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2021-10-27T06:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776682#M247053</link>
      <description>&lt;P&gt;Sort by numeric value then&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;order by input(scan(NAME,2,'_'),best.) , NAME;&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 27 Oct 2021 07:07:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776682#M247053</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-27T07:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776685#M247054</link>
      <description>it still does not work.&lt;BR /&gt;It says&lt;BR /&gt;" The query as specified involves ordering by an item that doesn't appear in its SELECT clause."&lt;BR /&gt;"Invalid string"&lt;BR /&gt;"Invalid argument to function INPUT. Missing values may be generated"</description>
      <pubDate>Wed, 27 Oct 2021 07:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776685#M247054</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2021-10-27T07:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776688#M247055</link>
      <description>&lt;P&gt;&lt;EM&gt;"Invalid argument to function INPUT. Missing values may be generated"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means the second part of the name is not always a number. Use a case statement to check the result of the scan function.&lt;/P&gt;
&lt;P&gt;Something like&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;order by case when compress(scan(NAME,2,'_'),,'d')=' ' then input(scan(NAME,2,'_'), best.) else 0 end, NAME;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 07:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776688#M247055</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-27T07:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776691#M247057</link>
      <description>&lt;P&gt;Another way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select trim(NAME) into :vars1 separated by ','
from HAVE
where compress(scan(NAME,2,'_'),,'d') ne ' '
   or scan(NAME,2,'_') eq ' '
sorted NAME;

select trim(NAME) into :vars2 separated by ','
from HAVE
where compress(scan(NAME,2,'_'),,'d') eq ' '
  and scan(NAME,2,'_') ne ' '
sorted by input(scan(NAME,2,'_'),best.), NAME;


create table WANT as
select &amp;amp;vars1, &amp;amp;vars2 from HAVE;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 07:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776691#M247057</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-27T07:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776692#M247058</link>
      <description>&lt;P&gt;Transpose the dataset, sort by _NAME_, then transpose back (if that is useful at all, in about 99% of cases a wide dataset is useless for further processing).&lt;/P&gt;
&lt;P&gt;Quick example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ _1 _5 _3;
datalines;
X 1 2 3
Y 6 5 4
;

proc transpose
  data=have
  out=long
;
by id;
var _:;
run;

data long2;
set long;
varnum = input(substr(_name_,2),best.);
run;

proc sort data=long2;
by id varnum;
run;

proc transpose
  data=long2
  out=want (drop=_name_)
;
by id;
var col1;
id _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Seriously consider what those _X variables contain, and if you're not better off by keeping the long dataset for further work, especially if you have lots of missing values.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 07:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776692#M247058</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-27T07:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Shows Variable XXX already exists on file work.XXX when I tried to reorder the variables by pref</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776699#M247059</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/398410"&gt;@ttqkroe&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a suitable ORDER BY clause in a PROC SQL step you can create a huge variety of sort orders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
someID='001';
array _[*] othervar v_20 abc_5 k_108 abc_20 v_108 v_5 k_5 abc_108 k_20 (0:9);
run;

/* Create list of variable names, ordered like this:
     1. names not containing an underscore, sorted by position in PDV
     2. names containing an underscore, sorted by numeric suffix, then by prefix
*/

proc sql noprint;
select name into :varlist separated by ' '
from dictionary.columns
where libname='WORK' &amp;amp; memname='HAVE'
order by find(name,'_')&amp;gt;0, varnum*(~find(name,'_')), input(scan(name,2,'_'),32.), name;
quit;

%put &amp;amp;varlist; /* for a visual check in the log */
%put %length(&amp;amp;varlist); /* should be &amp;lt;65533 */

/* Create dataset with sorted variables */

data want;
retain &amp;amp;varlist;
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Oct 2021 09:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shows-Variable-XXX-already-exists-on-file-work-XXX-when-I-tried/m-p/776699#M247059</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-27T09:06:13Z</dc:date>
    </item>
  </channel>
</rss>

