<?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: Import row 1 as name of the variable and row 2 as label in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343006#M272881</link>
    <description>&lt;P&gt;Here's how I would approach this.&lt;/P&gt;
&lt;P&gt;1) Save the Excel file to CSV.&lt;/P&gt;
&lt;P&gt;2) Make a two-row copy of the csv file to read just the varibles and labels.&lt;/P&gt;
&lt;P&gt;3) Import the two row file.&lt;/P&gt;
&lt;P&gt;4) Use the import wizard to import the main data file, make sure the get the names from the first row is checked and set the read data from row 3.&lt;/P&gt;
&lt;P&gt;5) If you are going to be importing multiple files with the same layout I &lt;STRONG&gt;strongly&lt;/STRONG&gt; suggest that you capture the datastep code from the log and save it for reuse. Check the lengths of character variables set by informat (and other informats assigned) you may want to change the lengths. &lt;STRONG&gt;Save&lt;/STRONG&gt; the code. You can read the next file and just change the infile and data set names (assumes you'll save further sets as CSV. Rational: If you use Proc Import everytime as a minimum you will get different lengths for character variables. In some case you may get variables changing type from character to numeric or vice versa.&lt;/P&gt;
&lt;P&gt;6) Use the data set from step 3 to&amp;nbsp;1) write a label statement to add to&amp;nbsp;your program (hint: file print, and array processing); 2) write proc datasets code to set&amp;nbsp;labels for the data set or 3) data step&amp;nbsp;with call execute to actually run the &amp;nbsp;proc datasets code. If the number of variables is small I'd likely type the label statement into the datastep code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This shows one way to write the label statement. The text will appear in the results or output, copy and paste into the editor. HAVE here is the data set from reading the first two rows so should have the same variable names as the main data.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set have (obs=1);
   array c {*} _character_;
   length _name_ $ 32 string $ 200;;
   file print;
   put "Label";

   do i= 1 to dim(c);
      _name_ = vname(c[i]);
      string = quote(strip(c[i]));
      put  _name_ "=" string;
   end;
   put ";";
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2017 17:36:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-03-21T17:36:38Z</dc:date>
    <item>
      <title>Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342943#M272878</link>
      <description>&lt;P&gt;Is there a way to import the first row as the name of the variable, the second row as the label for the variable, and the third rows and beyond as the data?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 14:41:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342943#M272878</guid>
      <dc:creator>kfwright23</dc:creator>
      <dc:date>2017-03-21T14:41:58Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342946#M272879</link>
      <description>&lt;P&gt;Not easily if that's what you are hoping for.&lt;/P&gt;
&lt;P&gt;Are you going to have to do this frequently to different files of different structures?&lt;/P&gt;
&lt;P&gt;Also what type of source file are you dealling with?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 14:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342946#M272879</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-21T14:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342966#M272880</link>
      <description>&lt;P&gt;The source file is excel. &amp;nbsp;I will not need to do this daily, but I will do it perioidically for survey data that I download from Qualtrics.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 15:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/342966#M272880</guid>
      <dc:creator>kfwright23</dc:creator>
      <dc:date>2017-03-21T15:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343006#M272881</link>
      <description>&lt;P&gt;Here's how I would approach this.&lt;/P&gt;
&lt;P&gt;1) Save the Excel file to CSV.&lt;/P&gt;
&lt;P&gt;2) Make a two-row copy of the csv file to read just the varibles and labels.&lt;/P&gt;
&lt;P&gt;3) Import the two row file.&lt;/P&gt;
&lt;P&gt;4) Use the import wizard to import the main data file, make sure the get the names from the first row is checked and set the read data from row 3.&lt;/P&gt;
&lt;P&gt;5) If you are going to be importing multiple files with the same layout I &lt;STRONG&gt;strongly&lt;/STRONG&gt; suggest that you capture the datastep code from the log and save it for reuse. Check the lengths of character variables set by informat (and other informats assigned) you may want to change the lengths. &lt;STRONG&gt;Save&lt;/STRONG&gt; the code. You can read the next file and just change the infile and data set names (assumes you'll save further sets as CSV. Rational: If you use Proc Import everytime as a minimum you will get different lengths for character variables. In some case you may get variables changing type from character to numeric or vice versa.&lt;/P&gt;
&lt;P&gt;6) Use the data set from step 3 to&amp;nbsp;1) write a label statement to add to&amp;nbsp;your program (hint: file print, and array processing); 2) write proc datasets code to set&amp;nbsp;labels for the data set or 3) data step&amp;nbsp;with call execute to actually run the &amp;nbsp;proc datasets code. If the number of variables is small I'd likely type the label statement into the datastep code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This shows one way to write the label statement. The text will appear in the results or output, copy and paste into the editor. HAVE here is the data set from reading the first two rows so should have the same variable names as the main data.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set have (obs=1);
   array c {*} _character_;
   length _name_ $ 32 string $ 200;;
   file print;
   put "Label";

   do i= 1 to dim(c);
      _name_ = vname(c[i]);
      string = quote(strip(c[i]));
      put  _name_ "=" string;
   end;
   put ";";
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 17:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343006#M272881</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-21T17:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343074#M272882</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Creating SAS labels using the second row of an excel sheet


HAVE AN EXCEL SHEET (where the second row is the label)
=======================================================

   +-------+-----------------+----------------+
   |   A   |        B        |        C       |
   +-------+-----------------+----------------+
1  | SEX   |        NAME     |       AGEC     |
2  |Gender |    Student Name |    Student Age |
   +-------+-----------------+----------------+
3  | M     |      Alfred     |       14       |
4  | F     |      Alice      |       13       |
5  | F     |      Barbara    |       13       |
6  | F     |      Carol      |       14       |
7  | M     |      Henry      |       14       |
8  | M     |      James      |       12       |
9  | F     |      Jane       |       12       |
10 | F     |      Janet      |       15       |
11 | M     |      Jeffrey    |       13       |
12 | M     |      John       |       12       |
13 | F     |      Joyce      |       11       |
14 | F     |      Judy       |       14       |
15 | F     |      Louise     |       12       |
16 | F     |      Mary       |       15       |
17 | M     |      Philip     |       16       |
18 | M     |      Robert     |       12       |
19 | M     |      Ronald     |       15       |
20 | M     |      Thomas     |       11       |
21 | M     |      William    |       15       |
   +-------+-----------------+----------------+

WANT
====
         Variables in Creation Order

#    Variable    Type    Len    Label

1    SEX         Char      6    Gender
2    NAME        Char     12    Student Name
3    AGEC        Char     11    Student Age


Up to 40 obs from want total obs=19

Obs   NAME       SEX  AGEC

  1   Alfred      M    14
  2   Alice       F    13
  3   Barbara     F    13
  4   Carol       F    14
  5   Henry       M    14
  6   James       M    12
  7   Jane        F    12
  8   Janet       F    15
  9   Jeffrey     M    13
 10   John        M    12
 11   Joyce       F    11
 12   Judy        F    14
 13   Louise      F    12
 14   Mary        F    15
 15   Philip      M    16
 16   Robert      M    12
 17   Ronald      M    15
 18   Thomas      M    11
 19   William     M    15


WORKING CODE
============

   dosubl(%nrbquote(
     lbl=catx(" ",lbl,cats(vname(names[i]),"=",'"',names[i],'"'));
     call symputx("lbl",lbl);

   label &amp;amp;lbl;

FULL SOLUTION
=============

*                _                  _
 _ __ ___   __ _| | _____     __  _| |___
| '_ ` _ \ / _` | |/ / _ \____\ \/ / / __|
| | | | | | (_| |   &amp;lt;  __/_____&amp;gt;  &amp;lt;| \__ \
|_| |_| |_|\__,_|_|\_\___|    /_/\_\_|___/

;

%utlfkil(d:/xls/rowlbl.xlsx);
libname xel "d:/xls/rowlbl.xlsx";

data xel.class(drop=age);
  length sex $8 name agec $18;
  set sashelp.class(keep=name sex age);
  agec=put(age,3.);
  savname=name;
  savage =agec;
  savsex =sex;
  if _n_=1 then do;
       name="Student Name";
       sex="Gender";      ;
       agec="Student Age"  ;
       output;
       name = savname;
       agec = savage ;
       sex  = savsex ;
       output;
   end;
   else output;
   drop
      savname
      savage
      savsex;
run;quit;

libname xel clear;

* __       _ _                 _       _   _
 / _|_   _| | |      ___  ___ | |_   _| |_(_) ___  _ __
| |_| | | | | |_____/ __|/ _ \| | | | | __| |/ _ \| '_ \
|  _| |_| | | |_____\__ \ (_) | | |_| | |_| | (_) | | | |
|_|  \__,_|_|_|     |___/\___/|_|\__,_|\__|_|\___/|_| |_|

;

/*
There are several solutions. You can read rectangles which
make it easier to have AGEC as a numeric.
You can get lengths using passthru
*/

libname xel "d:/xls/rowlbl.xlsx" scan_text=no header=no;

proc datasets lib=work kill;
run;quit;

%symdel nam lbl;

libname xel "d:/xls/rowlbl.xlsx";

data want;

 if _n_=0 then do;
    %let rc=%sysfunc(dosubl(%nrbquote(
        data _null_;
          set xel.class(firstobs=1 obs=1); * use "[sheet1$]" if you don't have a named range;
          array names _character_;
          length lbl $500;
          retain lbl;
          if _n_=1 then do;
              do i=1 to dim(names);
                lbl=catx(" ",lbl,cats(vname(names[i]),"=",'"',names[i],'"'));
              end;
              call symputx("lbl",lbl);
          end;
        run;quit;

    )));
  end;

  set xel.class(firstobs=2);

    length f: $8;
    format _all_;
    informat _all_;
    label &amp;amp;lbl;

run;quit;

libname xel clear;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2017 21:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343074#M272882</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-21T21:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343167#M272883</link>
      <description>&lt;PRE&gt;
How about this one.



proc import datafile='/folders/myfolders/test.xlsx' out=have dbms=xlsx replace;
 getnames=no;
run;

data x data;
 set have;
 if _n_ in (1 2) then output x;
  else output data;
run;
proc transpose data=x out=temp;
 var _all_;
run;

data _null_;
 set temp end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails;modify data;');
 call execute(cat('label ',_name_,'="',col2,'";'));
 call execute(cat('rename ',_name_,'=',col1,';'));
 if last then call execute('quit;');
run;
proc print data=data label;run;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Mar 2017 03:55:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343167#M272883</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-22T03:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343325#M272884</link>
      <description>&lt;P&gt;I did not try all solutions. &amp;nbsp;I tried this one and it worked great! &amp;nbsp;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 15:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343325#M272884</guid>
      <dc:creator>kfwright23</dc:creator>
      <dc:date>2017-03-22T15:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Import row 1 as name of the variable and row 2 as label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343572#M272885</link>
      <description>&lt;P&gt;Maybe sometimes you don't want transform character into num &amp;nbsp;by hand, try this one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc import datafile='/folders/myfolders/test.xlsx' out=x dbms=xlsx replace;
 getnames=no;
run;


proc import datafile='/folders/myfolders/test.xlsx' out=data dbms=xlsx replace;
 getnames=no;
 datarow=3;
run;

data x ;
 set x(obs=2);
run;
proc transpose data=x out=temp;
 var _all_;
run;

data _null_;
 set temp end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails;modify data;');
 call execute(cat('label ',_name_,'="',col2,'";'));
 call execute(cat('rename ',_name_,'=',col1,';'));
 if last then call execute('quit;');
run;
proc print data=data label;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Mar 2017 02:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-row-1-as-name-of-the-variable-and-row-2-as-label/m-p/343572#M272885</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-23T02:28:20Z</dc:date>
    </item>
  </channel>
</rss>

