<?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: Using external file to add variable label in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530761#M145164</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;I'd use call execute as well for his task.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jan 2019 20:30:18 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-01-28T20:30:18Z</dc:date>
    <item>
      <title>Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530423#M145051</link>
      <description>&lt;P&gt;I've a large data set (Data A, easily &amp;gt; 500K rows) with a long list of conditions&amp;nbsp; (A01...A38, B02... B75, C04..... S99). I want to add variable description using an external data set (Data B), with a corresponding list of variable names and description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I found a SAS code but it works on occasions, e.g. on A01-B20, but not when I tried to include all variables. I don't know why. Also I'm hoping the code can work efficiently as my data can be large.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The sample code is as below. The actual label data and the data to be applied (w/ 100 cases) are attached so that you can test the macro %&lt;CODE class=" language-sas"&gt;varlabel on them, and it may help to see why the macro works on the sample data but not the actual data&lt;/CODE&gt;. Thanks. Or any other SAS codes that would work for my purpose would do too. Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data labeldata; input var $ 1-3 labels $ 5-17;
datalines;
A01	Diebetes
A02 Heart disease
B01 Cancer
B02 Renal failure
;
data mydata; input ID 1 A01 3 A02 5 B01 7 B02 9;
datalines;
1 1 1 1 0
2 0 0 0 1
3 1 0 1 1
4 1 0 0 1
;
run;
%macro varlabel(indata=, lib=, labeldata=, labelvar=, labeldesc=);
data _null_; 
     set &amp;amp;labeldata; 
     call symput("var" || trim(left(_N_)), trim(left(&amp;amp;labelvar))); 
     call symput("label" || trim(left(_N_)), trim(left(&amp;amp;labeldesc))); 
     call symput("nobs", trim(left(_N_))); 
run;
proc datasets library = &amp;amp;lib memtype = data nolist; 
     modify &amp;amp;indata; 
     label 
          %do i = 1 %to &amp;amp;nobs; 
               &amp;amp;&amp;amp;var&amp;amp;i = &amp;amp;&amp;amp;label&amp;amp;i 
          %end; ; 
     quit;
run;
%mend;
%varlabel(indata=mydata, lib=work, labeldata=labeldata, labelvar=var, labeldesc=labels);
proc contents data=mydata order=varnum; run;
&lt;/CODE&gt;&lt;/PRE&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 06:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530423#M145051</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-27T06:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530430#M145057</link>
      <description>&lt;P&gt;Shouldn't it be&lt;/P&gt;
&lt;P&gt;&amp;amp;&amp;amp;var&amp;amp;i = "&amp;amp;&amp;amp;label&amp;amp;i"&lt;/P&gt;
&lt;P&gt;?&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 06:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530430#M145057</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-01-27T06:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530436#M145062</link>
      <description>&lt;P&gt;Thanks a lot. Indeed that's the reason. The code works now, with the fixes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlabel(indata=, lib=, labeldata=, labelvar=, labeldesc=);
* create distinct macro variables for each variable name and label; 
data _null_; 
     set &amp;amp;labeldata; 
     call symput("var" || trim(left(_N_)), trim(left(&amp;amp;labelvar))); 
     call symput("label" || trim(left(_N_)), trim(left(&amp;amp;labeldesc))); 
     call symput("nobs", trim(left(_N_))); 
run;
proc datasets library = &amp;amp;lib memtype = data nolist; 
     modify &amp;amp;indata; 
     label 
          %do i = 1 %to &amp;amp;nobs; 
               &amp;amp;&amp;amp;var&amp;amp;i = "&amp;amp;&amp;amp;label&amp;amp;i"

          %end; ; 
     quit;
run;
%mend;
%varlabel(indata=mydata, lib=work, labeldata=labeldata, labelvar=var, labeldesc=labels);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jan 2019 08:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530436#M145062</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-27T08:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530711#M145145</link>
      <description>&lt;P&gt;Instead of creating a bunch of macro variables and referencing issues you might consider CALL EXECUTE statements instead.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set &amp;amp;labeldata end=last;
   if _n_= 1 then do;
      call execute( "proc datasets library=&amp;amp;lib. memtype=data nolist;");
      call execute( "modify &amp;amp;indata.;");
      call execute( "label");
   end;
   call execute (var||'=' quote(strip(label)));
   if last call execute("quit;");
end;
&lt;/PRE&gt;
&lt;P&gt;And if your datalabel set actually had separate datasets then sorting the data by the dataset names, using BY&amp;nbsp; in the data _null_ with first.datasetname could be used to insert the modify statement for each data set, though you would use the variable name instead of &amp;amp;indata macro variable and the statement would be more like call execute ("modify "|| datasetnamevar ||";");&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 18:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530711#M145145</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-01-28T18:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530718#M145146</link>
      <description>&lt;P&gt;Putting the metadata into macro variables is just going to make it harder to deal.&amp;nbsp; Leave data in datasets.&lt;/P&gt;
&lt;P&gt;To add labels you want to generate label statement(s).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For that you just need your metadata file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data labeldata;
  infile cards truncover ;
   input  name :$32. label $255. ;
cards;
A01 Diabetes
A02 Heart disease
B01 Cancer
B02 Renal failure
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is probably easiest to write the label statement o a file than try to generate strings to push onto the program stack with CALL EXECUTE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename labels temp;
data _null_;
  set labeldata end=eof ;
  file labels;
  if _n_=1 then put 'label';
  put @3 name '=' label :$quote. ;
  if eof then put ';' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you just need to use %INCLUDE to use that code to modify the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets nolist libname=work;
modify mydata;
%include labels ;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 18:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530718#M145146</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-28T18:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using external file to add variable label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530761#M145164</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;I'd use call execute as well for his task.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 20:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-external-file-to-add-variable-label/m-p/530761#M145164</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-01-28T20:30:18Z</dc:date>
    </item>
  </channel>
</rss>

