<?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: Create a new column with label value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720358#M223148</link>
    <description>&lt;P&gt;Thanks, may you please show how to apply these solution here?&lt;/P&gt;</description>
    <pubDate>Thu, 18 Feb 2021 23:11:20 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-02-18T23:11:20Z</dc:date>
    <item>
      <title>Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720348#M223144</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to change the table from wide to long.&lt;/P&gt;
&lt;P&gt;It means that for each ID there will be 3 rows (with information of X1 X2 X3 ).&lt;/P&gt;
&lt;P&gt;I have 3 queations:&lt;/P&gt;
&lt;P&gt;1-Since X3 is numeric and X1,X2 are char, should I need to do any convertion (Because in the long structure the values of X1 ,X2,X3 should be in same column)&lt;/P&gt;
&lt;P&gt;2-Is there a better way to create the long structure table?&lt;/P&gt;
&lt;P&gt;3-I want to add a new column to&amp;nbsp;&lt;CODE class=" language-sas"&gt;LongTbl that is called labelField that&amp;nbsp;will&amp;nbsp;get&amp;nbsp;the value of label of X1/X2/X3.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;What is the way to do it please in a way that it takes the label automatically ?(Here&amp;nbsp;I&amp;nbsp;type&amp;nbsp;it&amp;nbsp;but&amp;nbsp;in&amp;nbsp;real&amp;nbsp;life&amp;nbsp;I&amp;nbsp;have&amp;nbsp;many&amp;nbsp;varaibles&amp;nbsp;and&amp;nbsp;I&amp;nbsp;dont&amp;nbsp;want&amp;nbsp;to&amp;nbsp;type&amp;nbsp;the&amp;nbsp;label&amp;nbsp;for&amp;nbsp;each&amp;nbsp;varaible&amp;nbsp;and&amp;nbsp;I&amp;nbsp;prefer&amp;nbsp;to&amp;nbsp;find&amp;nbsp;a&amp;nbsp;clever&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data WideTbl;
input ID date : date9. x1 $ x2  $ x3  ;
format date ddmmyy10.;
label x1='Team' x2='smoker' X3='status';
cards;
1 01jan2021 a y 1
2 01jan2021 b n 2
3 01jan2021 c n 2
4 01jan2021 a n 3
5 02jan2021 b y 1
6 02jan2021 a y 1
7 03jan2021 c y 2
8 03jan2021 a n 2
9 03jan2021 a y 3
;
run;

data LongTbl;
 set WideTbl;
     length category $3;
     category=x1; field='x1';labelField ='Team'; output;
     category=x2; field='x2'; labelField ='smoker';output;
     category=x3; field='x3';labelField ='status'; output;
     drop x1 - x3;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2021 22:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720348#M223144</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-18T22:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720352#M223146</link>
      <description>&lt;P&gt;1. Yes, you'll need to convert your variables to the same type.&lt;/P&gt;
&lt;P&gt;2. No, since you need to do type conversion, you may as well do it on in a data step&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;VLABEL() and VNAME() functions are what you're looking for here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC TRANSPOSE has the IDLABEL variable but not sure how that works when you go wide to long.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;Wide to Long:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;And sometimes a double transpose is needed for extra wide data sets:&lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2021 23:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720352#M223146</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-18T23:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720358#M223148</link>
      <description>&lt;P&gt;Thanks, may you please show how to apply these solution here?&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2021 23:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720358#M223148</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-18T23:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720360#M223149</link>
      <description>&lt;P&gt;Will it work well?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data WideTbl;
input ID date : date9. x1 $ x2  $ x3  ;
format date ddmmyy10.;
label x1='Team' x2='smoker' X3='status';
cards;
1 01jan2021 a y 1
2 01jan2021 b n 2
3 01jan2021 c n 2
4 01jan2021 a n 3
5 02jan2021 b y 1
6 02jan2021 a y 1
7 03jan2021 c y 2
8 03jan2021 a n 2
9 03jan2021 a y 3
;
run;

/*To convert X3 to char*/
Data WideTbl2;
set WideTbl;
X3_char=compress(X3);
drop X3;
rename X3_char=X3;
Run;


data LongTbl;
set WideTbl;
length category $3 labelField $20. ;
 field='x1';category=x1;labelField = vlabel(x1);output;
 field='x2';category=x2;labelField = vlabel(x2);output;
 field='x3';category=x3;labelField = vlabel(x3);output;
 drop x1 - x3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2021 23:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720360#M223149</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-18T23:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720363#M223152</link>
      <description>&lt;P&gt;You really should avoid automatic numeric to character conversions that your code used. Explicitly PUT the numeric with an appropriate format AND add the -L modifier to left justify the value unless you want one or more leading spaces.&lt;/P&gt;
&lt;PRE&gt;data LongTbl;
set WideTbl;
length category $3;
 field='x1';category=x1;labelField = vlabel(x1);output;
 field='x2';category=x2;labelField = vlabel(x2);output;
 field='x3';category=put(x3,best. -L);labelField = vlabel(x3);output;
 drop x1 - x3;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2021 23:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720363#M223152</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-18T23:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720374#M223158</link>
      <description>&lt;P&gt;Since you have unique BY variables just use PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WideTbl;
  input ID date : date9. x1 $ x2  $ x3  ;
  format date ddmmyy10.;
  label x1='Team' x2='smoker' X3='status';
cards;
1 01jan2021 a y 1
2 01jan2021 b n 2
3 01jan2021 c n 2
4 01jan2021 a n 3
5 02jan2021 b y 1
6 02jan2021 a y 1
7 03jan2021 c y 2
8 03jan2021 a n 2
9 03jan2021 a y 3
;

proc transpose data=widetbl 
  out=LongTbl(rename=(_name_=field _label_=labelField col1=category))
;
  by id date;
  var x1-x3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have mixed numeric and character then CATEGORY will automatically become character.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                                    label
Obs    ID          date    field    Field       category

  1     1    01/01/2021     x1      Team      a
  2     1    01/01/2021     x2      smoker    y
  3     1    01/01/2021     x3      status               1
  4     2    01/01/2021     x1      Team      b
  5     2    01/01/2021     x2      smoker    n
  6     2    01/01/2021     x3      status               2
  7     3    01/01/2021     x1      Team      c
  8     3    01/01/2021     x2      smoker    n
  9     3    01/01/2021     x3      status               2
&lt;/PRE&gt;
&lt;P&gt;But you might want to add a step to left justify the strings generated from the numeric variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data LongTbl; set LongTbl;
  category=left(category);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                    label
Obs    ID          date    field    Field     category

  1     1    01/01/2021     x1      Team         a
  2     1    01/01/2021     x2      smoker       y
  3     1    01/01/2021     x3      status       1
  4     2    01/01/2021     x1      Team         b
  5     2    01/01/2021     x2      smoker       n
  6     2    01/01/2021     x3      status       2
  7     3    01/01/2021     x1      Team         c
  8     3    01/01/2021     x2      smoker       n
  9     3    01/01/2021     x3      status       2
&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Feb 2021 01:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720374#M223158</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-19T01:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720631#M223237</link>
      <description>Thank you,May I ask some questions please:&lt;BR /&gt;What is the problem with compress(x3)?&lt;BR /&gt;What problem can be caused by using it?&lt;BR /&gt;Is PUT(X3,best.,-L)  equivalent to LEFT(PUT(X3,best.)?</description>
      <pubDate>Sat, 20 Feb 2021 09:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720631#M223237</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-20T09:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720632#M223238</link>
      <description>Thank you,&lt;BR /&gt;As I see you didnt use Idlabel in proc transpose.  why?&lt;BR /&gt;How did you tell SAS to create a varaible with label value?&lt;BR /&gt;Thank you</description>
      <pubDate>Sat, 20 Feb 2021 10:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720632#M223238</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-20T10:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new column with label value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720697#M223270</link>
      <description>&lt;P&gt;The IDLABEL statement is for transposing the other way, when you are using the it to make new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fact that at least one of the variables being transposed had a LABEL attached to it is what caused PROC TRANSPOSE to create the _LABEL_ variable.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2021 01:29:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-column-with-label-value/m-p/720697#M223270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-21T01:29:51Z</dc:date>
    </item>
  </channel>
</rss>

