<?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: Select variables based on their column number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332547#M74874</link>
    <description>&lt;P&gt;Well, your&amp;nbsp;&lt;STRONG&gt;best&lt;/STRONG&gt; options would be to drop Excel as a data transfer medium - this would fix all your problems, make a far more robust data transfer system, and make your life easier. &amp;nbsp;However you will say that you can't and so your process will always be sub-par. &amp;nbsp;You can get a list of varaible names via:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select  NAME
  into     :VLIST separated by " "
  from    DICTIONARY.COLUMNS
  where  LIBNAME="&amp;lt;thelibname&amp;gt;"
     and  MEMNAME="&amp;lt;dataste name&amp;gt;";
quit;

...
  var &amp;amp;varlist.;
...&lt;/PRE&gt;
&lt;P&gt;I can think of at least a few issues with the process - transposing, what happens when there are mixed number/character and if you let SAS auto convert it will it be right. &amp;nbsp;How will you validate such a process? &amp;nbsp;What happens when the spreadsheet changes - i.e. your four always there variables - you don't have a signed import agreement so they could send anything they like. &amp;nbsp;Etc.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Feb 2017 11:01:27 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-02-14T11:01:27Z</dc:date>
    <item>
      <title>Select variables based on their column number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332539#M74872</link>
      <description>&lt;P&gt;Dear SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a new user of SAS and I guess this is a fairly easy question but I have been searching the web and I can't solve it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I receive a file on a weekly basis containing trainings that employees have to take&amp;nbsp;and I need to pivot some of its columns. The first 4 variables are always called the same (they are basic employees info), but the rest varies (the variables names change and I have a different number of columns each time, as each week different trainings have to be taken). Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;IMPORT&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;OUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;= IMPORT.Mandatory_Trainings&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;DATAFILE&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;= &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"Excel file path"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;DBMS&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=EXCEL REPLACE;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;Range&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"A1:AAA100000"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;GETNAMES&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=Yes; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;MIXED&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=NO;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;SCANTEXT&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=YES;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;USEDATE&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=YES;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;SCANTIME&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=YES;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;transpose&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;Data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=IMPORT.Mandatory_Trainings &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;OUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=Base.Mandatory_Trainings_Pivot;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;BY&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; ID Employee_Name Overall__Training_Status General_Exclusion__Status; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;VAR&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; XXXXX;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;The line "VAR XXXXX;" is where I need to change my code.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;I put the name of the first and last training and separated them with "--" ("First_Training_Name -- Last_Training_Name_"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) and it worked perfectly fine but I want a more automated way where SAS select and pivot&amp;nbsp;all variables that are between column 5 and the last one. Any advice on how to do this?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Thanks in advance for the help.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 10:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332539#M74872</guid>
      <dc:creator>rlolo</dc:creator>
      <dc:date>2017-02-14T10:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Select variables based on their column number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332547#M74874</link>
      <description>&lt;P&gt;Well, your&amp;nbsp;&lt;STRONG&gt;best&lt;/STRONG&gt; options would be to drop Excel as a data transfer medium - this would fix all your problems, make a far more robust data transfer system, and make your life easier. &amp;nbsp;However you will say that you can't and so your process will always be sub-par. &amp;nbsp;You can get a list of varaible names via:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select  NAME
  into     :VLIST separated by " "
  from    DICTIONARY.COLUMNS
  where  LIBNAME="&amp;lt;thelibname&amp;gt;"
     and  MEMNAME="&amp;lt;dataste name&amp;gt;";
quit;

...
  var &amp;amp;varlist.;
...&lt;/PRE&gt;
&lt;P&gt;I can think of at least a few issues with the process - transposing, what happens when there are mixed number/character and if you let SAS auto convert it will it be right. &amp;nbsp;How will you validate such a process? &amp;nbsp;What happens when the spreadsheet changes - i.e. your four always there variables - you don't have a signed import agreement so they could send anything they like. &amp;nbsp;Etc.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 11:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332547#M74874</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-14T11:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select variables based on their column number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332597#M74884</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Select excel variables based on their column number to piviot

I assume variables to transform are after 'ID' variables.

HAVE (Two workbooks)

d:/xls/class.xlsx

 +-------------------------+------+-----------+------------
 |      |    A      |   B  |   B  |    C      |    D      |
 +------+-----------+------+------+-----------+-----------+
 |      |           |      |      |           |           |
 |    1 |   NAME    |   AGE|  SEX |  HEIGHT   |  WEIGHT   |
 |    2 |   Alfred  |   14 |   M  |   55      |   96      |
 |    3 |   Alice   |   13 |   F  |   44      |   87      |
 |    4 |   Barbara |   13 |   F  |   48      |   88      |
 |    5 |   Carol   |   14 |   F  |   56      |   99      |
 |    6 |   Henry   |   14 |   M  |   52      |   84      |
 | ...  |   ...     |  ... | ...  |   ..      |   ...     |
 +------------------+------+------+-----------+-----------+

 [CLASS}


d:/xls/class.xlsx

 +-------------------------+------+-----------+-----------+-----------+
 |      |    A      |   B  |   B  |    C      |    D      |   E       |
 +------+-----------+------+------+-----------+-----------+-----------+
 |      |           |      |      |           |           |           |
 |    1 |   NAME    |   AGE|  SEX |  HEIGHT   |  WEIGHT   |  PREDICT  |
 |    2 |   Alfred  |   14 |   M  |   55      |   96      |   91      |
 |    3 |   Alice   |   13 |   F  |   44      |   87      |   82      |
 |    4 |   Barbara |   13 |   F  |   48      |   88      |   83      |
 |    5 |   Carol   |   14 |   F  |   56      |   99      |   94      |
 |    6 |   Henry   |   14 |   M  |   52      |   84      |   85      |
 | ...  |   ...     |  ... | ...  |   ..      |   ...     |   ...     |
 +------------------+------+------+-----------+-----------+-----------+

 [CLASSFIT}

WANT

Up to 40 obs from WORK.CLASSXPO total obs=95

Obs    NAME       _NAME_      COL1

  1    Alfred     NAME        Alfred
  2    Alfred     SEX         M
  3    Alfred     AGE         14
  4    Alfred     HEIGHT      69
  5    Alfred     WEIGHT      112.5
  6    Alice      NAME        Alice
  7    Alice      SEX         F
  8    Alice      AGE         13
  9    Alice      HEIGHT      56.5
 10    Alice      WEIGHT      84
...

Obs    NAME       _NAME_       COL1

  1    Alfred     NAME         Alfred
  2    Alfred     SEX          M
  3    Alfred     AGE          14
  4    Alfred     HEIGHT       69
  5    Alfred     WEIGHT       112.5
  6    Alfred     PREDICT      126
 11    Alice      NAME         Alice
 12    Alice      SEX          F
 13    Alice      AGE          13
 14    Alice      HEIGHT       56.5
 15    Alice      WEIGHT       84
 16    Alice      PREDICT      77
....

WORKING CODE
============

      proc transpose data=xel.&amp;amp;bok out=&amp;amp;bok.xpo(drop=_label_);
         by name;
         var _all_;

FULL SOLUTION
=============

* create some code;
data _null_;
  do wrkbok="classfit","class";
    call symputx('bok',wrkbok);
    rc=dosubl('
      %*utlfkil(d:/xls/&amp;amp;bok..xlsx);
      libname xel "d:/xls/&amp;amp;bok..xlsx";
      data xel.&amp;amp;bok;
         set sashelp.&amp;amp;bok;
      run;quit;
      libname xel clear;
    ');
  end;
run;quit;

* transpose;
data _null_;
  do wrkbok="classfit","class";
    call symputx('bok',wrkbok);
    rc=dosubl('
      libname xel "d:/xls/&amp;amp;bok..xlsx";
      proc transpose data=xel.&amp;amp;bok out=&amp;amp;bok.xpo(drop=_label_);
         by name;
         var _all_;
      run;quit;
      libname xel clear;
    ');
  end;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2017 13:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332597#M74884</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-14T13:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Select variables based on their column number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332600#M74885</link>
      <description>&lt;P&gt;You&amp;nbsp; could put this DATA step between the import and the transpose - in which you&amp;nbsp;insert 2 sentinel variables (_PRE and _POST) in location 5 and location "last".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need;
  set IMPORT.Mandatory_Trainings (keep=ID Employee_Name Overall__Training_Status General_Exclusion__Status);
  retain _PRE .;
  set IMPORT.Mandatory_Trainings;
  retain _POST .;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "trick" here is to understand how SAS compiler builds the program data vector (PDV) in a data step.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The first SET keeps only a limited set of 4 variables, which become the 4 left-most in the PDV.&lt;/LI&gt;
&lt;LI&gt;Then the retain&amp;nbsp;_PRE statement appends it to the right, i.e. column 5.&lt;/LI&gt;
&lt;LI&gt;Then a second SET reads all the training vars.&amp;nbsp; Four of them are already on the&amp;nbsp;left of _PRE, but the rest are appended to the right of _PRE.&lt;/LI&gt;
&lt;LI&gt;Then the retain _POST generates the right-most var.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then you can modify the proc transpose to always have "var _PRE--_POST" - no changing varname awareness required.&amp;nbsp;&amp;nbsp; And just insert a &lt;EM&gt;&lt;STRONG&gt;"(where=(not(name in ("_PRE","_POST"))))"&lt;/STRONG&gt;&lt;/EM&gt; dataset-name-parameter in the "out=" clause:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose Data=need
  OUT=Base.Mandatory_Trainings_Pivot (where=(not(_name_ in ('_PRE','_POST'))));
  BY ID Employee_Name Overall__Training_Status General_Exclusion__Status; 
  var _pre -- _post;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2017 13:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-variables-based-on-their-column-number/m-p/332600#M74885</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-14T13:35:28Z</dc:date>
    </item>
  </channel>
</rss>

