<?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: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889915#M39563</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446556"&gt;@HijB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset with 250 binary variables:&lt;/P&gt;
&lt;P&gt;is there a way replace any value of 1 to the name of the variable itself ,so the data eventually looks like the table below, without having to do each variable individually?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? Describe an actual use case for adding 250 variables that basically copy an existing variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of using your data to create custom formats that will display that text as requested:&lt;/P&gt;
&lt;PRE&gt;data health;
  input ID	safe	house	food;
cards;
1	1	.	.	 	 	 
2	.	1	1	  
3	.	1	.	
4	1	.	1	 	 	 
;
run;

data use;
   /* you would only use the list of variables that you want*/
   /* the -- is for variables in adjacent columns to create a list*/
   set health (keep= safe -- food obs=1);
   array n (*) _numeric_;
   length hlo $ 3; 
   do i=1 to dim(n);
      fmtname=vname(n[i]);
      start=1;
      type='N';
      label= fmtname;
      hlo='';
      output;
      start=.;
      label=' ';
      hlo='O';
      output;
   end;
   keep fmtname start type label hlo;
run;

proc format cntlin=use cntlout=useout;
run;

proc print data=health;
   format safe safe. house house. food food.;
run;
      &lt;/PRE&gt;
&lt;P&gt;You can also use the approach in the USE data set to create a text format assignment statement to associate the formats with variables.&lt;/P&gt;
&lt;P&gt;Warning: likely will have problems if you have any name literals where you use "this is stupid var"n .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Aug 2023 15:29:22 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-08-18T15:29:22Z</dc:date>
    <item>
      <title>how can I turn a numeric variable to a string; turning 1 value to the name of the variable itself</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889873#M39557</link>
      <description>&lt;P&gt;I have a dataset with 250 binary variables:&lt;/P&gt;&lt;P&gt;is there a way replace any value of 1 to the name of the variable itself ,so the data eventually looks like the table below, without having to do each variable individually?&lt;/P&gt;&lt;P&gt;PS: I want to&amp;nbsp; keep both the original numeric variables and the new string variables&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data health;
  input ID	safe	house	food;
cards;
1	1	.	.	 	 	 
2	.	1	1	  
3	.	1	.	
4	1	.	1	 	 	 
;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;outcome table:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;safe&lt;/TD&gt;&lt;TD&gt;house&lt;/TD&gt;&lt;TD&gt;food&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;safe&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;house&lt;/TD&gt;&lt;TD&gt;food&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;house&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;safe&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;food&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2023 13:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889873#M39557</guid>
      <dc:creator>HijB</dc:creator>
      <dc:date>2023-08-18T13:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889878#M39559</link>
      <description>&lt;P&gt;You obviously cannot put "FOOD" into a numeric variable.&amp;nbsp; So you will need to make new character variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data health;
  input ID safe house food;
cards;
1  1  .  .
2  .  1  1
3  .  1  .
4  1  .  1
;

data want;
  set health;
  array nums safe house food;
  array names[3] $32 ;
  do index=1 to 3;
    if nums[index] then names[index]=vname(nums[index]);
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 13:46:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889878#M39559</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-18T13:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889890#M39562</link>
      <description>&lt;P&gt;Use the opportunity to get a long layout, which is better for future analysis:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array vars {*} safe--food;
length value $32;
do i = 1 to dim(vars);
  if vars{i} ne.
  then do;
    value = vname(vars{i});
    output;
  end;
end;
keep id value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 14:10:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889890#M39562</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-18T14:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889915#M39563</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446556"&gt;@HijB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset with 250 binary variables:&lt;/P&gt;
&lt;P&gt;is there a way replace any value of 1 to the name of the variable itself ,so the data eventually looks like the table below, without having to do each variable individually?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? Describe an actual use case for adding 250 variables that basically copy an existing variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of using your data to create custom formats that will display that text as requested:&lt;/P&gt;
&lt;PRE&gt;data health;
  input ID	safe	house	food;
cards;
1	1	.	.	 	 	 
2	.	1	1	  
3	.	1	.	
4	1	.	1	 	 	 
;
run;

data use;
   /* you would only use the list of variables that you want*/
   /* the -- is for variables in adjacent columns to create a list*/
   set health (keep= safe -- food obs=1);
   array n (*) _numeric_;
   length hlo $ 3; 
   do i=1 to dim(n);
      fmtname=vname(n[i]);
      start=1;
      type='N';
      label= fmtname;
      hlo='';
      output;
      start=.;
      label=' ';
      hlo='O';
      output;
   end;
   keep fmtname start type label hlo;
run;

proc format cntlin=use cntlout=useout;
run;

proc print data=health;
   format safe safe. house house. food food.;
run;
      &lt;/PRE&gt;
&lt;P&gt;You can also use the approach in the USE data set to create a text format assignment statement to associate the formats with variables.&lt;/P&gt;
&lt;P&gt;Warning: likely will have problems if you have any name literals where you use "this is stupid var"n .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2023 15:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889915#M39563</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-18T15:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889967#M39568</link>
      <description>&lt;P&gt;Might be simpler to not try to replicate the same number of variables.&lt;/P&gt;
&lt;P&gt;Then you can just use a couple of PROC TRANSPOSE steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data health;
  input ID safe house food;
cards;
1 1 . . 
2 . 1 1 
3 . 1 . 
4 1 . 1 
;

proc transpose data=health out=tall  ;
  by id;
run;

proc transpose data=tall(where=(col1)) out=wide(drop=_name_ _label_) prefix=issue;
  by id;
  var _name_;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID    issue1    issue2

 1      1    safe
 2      2    house      food
 3      3    house
 4      4    safe       food

&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 18:11:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/889967#M39568</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-18T18:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: how can I turn a numeric variable to a string; turning 1 value to the name of the variable itsel</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/890082#M39585</link>
      <description>&lt;P&gt;Ideally transpose your source data from a wide to a long data structure which is most of the time easier to work with.&lt;/P&gt;
&lt;P&gt;There is no need to create a full set of additional string variables if it's just about printing/reporting. You could generate and use formats instead as done in below sample code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
  input ID safe house food;
cards;
1 1 . .      
2 . 1 1   
3 . 1 . 
4 1 . 1      
;
run;

proc sql noprint;
  select 
    catx(' ','value',name,"1='",name,"';"),
    catx(' ',name,cats(name,'.'))
      into 
        :fmt_def separated by ' ',
        :fmt_apply separated by' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE' and upcase(name) ne 'ID'
  ;
quit;

proc format;
  &amp;amp;fmt_def;
run;

/*** proc dataset if you want to apply the formats permanently ***/
/*proc datasets lib=work nolist;*/
/*  modify have;*/
/*    format &amp;amp;fmt_apply;*/
/*  run;*/
/*quit;*/

proc print data=have;
  /* format statement here only required if formats not applied permanently */
  format &amp;amp;fmt_apply;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1692501302413.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86843i3EF2677FF3AB59DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1692501302413.png" alt="Patrick_0-1692501302413.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please note that variables Safe, House and Food are still numeric and the internal values stored are still 1 or missing. The generated formats applied just change how the values get printed.&lt;/P&gt;
&lt;P&gt;If you apply the formats permanently then you also need to store the compiled format definitions permanently or you need to re-create them whenever you want to use them.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2023 03:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-I-turn-a-numeric-variable-to-a-string-turning-1-value-to/m-p/890082#M39585</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-20T03:21:12Z</dc:date>
    </item>
  </channel>
</rss>

