<?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: sorting columns using macro variable problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485536#M126162</link>
    <description>&lt;P&gt;Thanks everyone.&amp;nbsp; It was due to the non-standard naming of my variable names, and nliteral() solved the issue.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Aug 2018 17:34:54 GMT</pubDate>
    <dc:creator>sunless07652</dc:creator>
    <dc:date>2018-08-09T17:34:54Z</dc:date>
    <item>
      <title>sorting columns using macro variable problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485309#M126066</link>
      <description>&lt;P&gt;Hi, I'm trying to reorder the columns of a dataset using proc sql and a macro variable containing the sorted list of variables I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc transpose data = WORK.MEMBERS OUT=WORK.SUMMARY prefix=svcmth_;&lt;BR /&gt;BY MEMBER_MAID MEMBER_ID RegionName_N;&lt;BR /&gt;VAR SUM_of_Month_Paid;&lt;BR /&gt;ID svc_mth;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my table SUMMARY after the proc transpose has fields:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name Type Length Format Informat Label&lt;BR /&gt;MEMBER_MAID Character 13 $CHAR13. $CHAR13.&lt;BR /&gt;MEMBER_ID Character 8 $CHAR8. $CHAR8.&lt;BR /&gt;RegionName_N Character 13 $CHAR13. $CHAR13.&lt;BR /&gt;_NAME_ Character 17 NAME OF FORMER VARIABLE&lt;BR /&gt;svcmth_2018-01 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-07 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-10 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-07 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-01 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-04 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2018-04 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-08 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-12 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-11 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-02 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-03 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-06 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-10 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2018-03 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2018-06 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-11 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2018-02 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-09 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-12 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-08 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2018-05 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2017-05 Numeric 8 BEST12.&lt;BR /&gt;svcmth_2016-09 Numeric 8 BEST12.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc contents data = WORK.SUMMARY out=col_names (keep=name);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data = col_names;&lt;BR /&gt;by name;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data col_names;&lt;BR /&gt;set col_names;&lt;/P&gt;&lt;P&gt;if name in ('MEMBER_ID', 'MEMBER_MAID', 'RegionName_N', '_NAME_') then delete;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set col_names;&lt;BR /&gt;BY NAME;&lt;BR /&gt;RETAIN sorted_cols;&lt;BR /&gt;length sorted_cols $2500.;&lt;BR /&gt;if _n_ =1 then sorted_cols = name;&lt;BR /&gt;else sorted_cols = catx(', ', sorted_cols, name);&lt;BR /&gt;call symput('sorted_cols', sorted_cols);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%put &amp;amp;sorted_cols;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table SUMMARY2 as&lt;BR /&gt;select MEMBER_ID, MEMBER_MAID, RegionName_N, &amp;amp;sorted_cols&lt;BR /&gt;from SUMMARY;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS gives me an error on the last proc sql step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;79 %put &amp;amp;sorted_cols;&lt;BR /&gt;svcmth_2016-07, svcmth_2016-08, svcmth_2016-09, svcmth_2016-10, svcmth_2016-11, svcmth_2016-12, svcmth_2017-01, svcmth_2017-02,&lt;BR /&gt;svcmth_2017-03, svcmth_2017-04, svcmth_2017-05, svcmth_2017-06, svcmth_2017-07, svcmth_2017-08, svcmth_2017-09, svcmth_2017-10,&lt;BR /&gt;svcmth_2017-11, svcmth_2017-12, svcmth_2018-01, svcmth_2018-02, svcmth_2018-03, svcmth_2018-04, svcmth_2018-05, svcmth_2018-06&lt;BR /&gt;80&lt;BR /&gt;81 proc sql;&lt;BR /&gt;82 create table op_summary2 as&lt;BR /&gt;83 select MEMBER_ID, MEMBER_MAID, RegionName_N, &amp;amp;sorted_cols&lt;BR /&gt;84 from OP_SUMMARY;&lt;BR /&gt;ERROR: The following columns were not found in the contributing tables: svcmth_2016, svcmth_2017, svcmth_2018.&lt;BR /&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;BR /&gt;85 quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't understand&amp;nbsp;why it's giving me this error, especially since the&amp;nbsp;%put statement shows that the macro variable "&lt;SPAN&gt;sorted_cols" does not contain these columns (svcmth_2016, svcmth_2017, svcmth_2018).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 23:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485309#M126066</guid>
      <dc:creator>sunless07652</dc:creator>
      <dc:date>2018-08-08T23:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: sorting columns using macro variable problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485321#M126070</link>
      <description>&lt;P&gt;You are trying to use non-standard variable names, since a dash is not normally a valid character when naming a variable.&amp;nbsp; You will have to change the way you refer to those variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doesn't work:&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;svcmth_2018-01&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Works:&amp;nbsp; "&lt;SPAN&gt;svcmth_2018-01"n&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Another option needs to be in place to allow these non-standard names.&amp;nbsp; I think it looks like this:&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;options validvarname=any;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It shouldn't be too difficult to get your SQL code to produce a macro variable with the proper form for the variable names.&amp;nbsp; Instead of selecting NAME, you would need to select an expression that includes the name in double quotes, with the letter n following.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Alternatively, you could take the simple route and change the values of SVC_MTH.&amp;nbsp; Either removing the dashes, or replacing them with underscores would work.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 00:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485321#M126070</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-08-09T00:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: sorting columns using macro variable problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485326#M126073</link>
      <description>&lt;P&gt;The nliteral() function will convert non-standard names to name literals.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 01:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485326#M126073</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-09T01:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: sorting columns using macro variable problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485336#M126079</link>
      <description>&lt;P&gt;During a transpose, SAS creates the new columns in the order in which it meets the ID values. So if all columns are represented in your first MEMBER_MAID/MEMBER_ID/RegionName_N by-group, you can get sorted columns directly by sorting ID values prior to transposing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=MEMBERS; by MEMBER_MAID MEMBER_ID RegionName_N svc_mth; run;

proc transpose data=MEMBERS OUT=SUMMARY prefix=svcmth_;
BY MEMBER_MAID MEMBER_ID RegionName_N;
VAR SUM_of_Month_Paid;
ID svc_mth;
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 03:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485336#M126079</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-09T03:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: sorting columns using macro variable problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485536#M126162</link>
      <description>&lt;P&gt;Thanks everyone.&amp;nbsp; It was due to the non-standard naming of my variable names, and nliteral() solved the issue.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 17:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-columns-using-macro-variable-problem/m-p/485536#M126162</guid>
      <dc:creator>sunless07652</dc:creator>
      <dc:date>2018-08-09T17:34:54Z</dc:date>
    </item>
  </channel>
</rss>

