<?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: Convert all numeric variables to character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444213#M111173</link>
    <description>&lt;P&gt;Back to basic,you need new assignment and can;t change existing mate i.e you need a new array&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input ID YEAR Manager $  TURNOVER;
  drop TURNOVER;
datalines;
1 1990 a 0
1 1991 a 0
1 1992 b 1
1 1993 b 0
1 1994 b 0
1 1995 c 1
1 1996 c 0
1 1997 c 0
2 1993 d 0
2 1994 d 0
2 1995 d 0
2 1996 e 1
2 1997 e 0
2 1998 e 0
2 1999 e 0
2 2000 e 0
2 2001 e 0
;
run;



data convertnums;
	set sashelp.class; 
	array all_n[*] _numeric_ ;
	array t(3) $ _age _height _weight;
	do i = 1 to dim(all_n); 
		t[i] = strip(put(all_n[i], 8.));  
	end;
run; 
proc contents data = convertnums;   * Check to see if numeric type converted ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Mar 2018 18:59:55 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-03-09T18:59:55Z</dc:date>
    <item>
      <title>Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444205#M111167</link>
      <description>&lt;P&gt;I find a lot of great resources for converting character variables to numeric, but I don't find much in terms of doing the reverse. I'm interested in identifying all numeric variable types and converting them to character type. Moreover, I'm interested in keeping the same variable names.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my thought process:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data convertnums;
	set sashelp.class; 
	array all_n[*] _numeric_ ;
	do i = 1 to dim(all_n); 
		all_n[i] = strip(put(all_n[i], $8.));  * Not working... decode; 
	end;
run; 

proc contents data = convertnums;   * Check to see if numeric type converted ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I am getting a warning and am unable to convert numeric to character. Any thoughts or suggestions are welcome. Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 18:29:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444205#M111167</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-03-09T18:29:25Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444208#M111169</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*&lt;SPAN&gt;macro to convert all numeric to char*/&lt;/SPAN&gt;&lt;BR /&gt;%macro vars(dsn);
  %let list=;
  %let type=;
  %let dsid=%sysfunc(open(&amp;amp;dsn));
  %let cnt=%sysfunc(attrn(&amp;amp;dsid,nvars));
   %do i = 1 %to &amp;amp;cnt;
    %let list=&amp;amp;list %sysfunc(varname(&amp;amp;dsid,&amp;amp;i));
    %let type=&amp;amp;type %sysfunc(vartype(&amp;amp;dsid,&amp;amp;i));
   %end;
  %let rc=%sysfunc(close(&amp;amp;dsid));
  data want(drop=
    %do i = 1 %to &amp;amp;cnt;
     %let temp=%scan(&amp;amp;list,&amp;amp;i);
       _&amp;amp;temp
    %end;);
   set &amp;amp;dsn(rename=(
    %do i = 1 %to &amp;amp;cnt;
     %let temp=%scan(&amp;amp;list,&amp;amp;i);
       &amp;amp;temp=_&amp;amp;temp
    %end;));
    %do j = 1 %to &amp;amp;cnt;
     %let temp=%scan(&amp;amp;list,&amp;amp;j);
   /** Change C to N for numeric to character conversion  **/
     %if %scan(&amp;amp;type,&amp;amp;j) = N %then %do;
   /** Also change INPUT to PUT for numeric to character  **/
      &amp;amp;temp=PUT(_&amp;amp;temp,8.);
     %end;
     %else %do;
      &amp;amp;temp=_&amp;amp;temp;
     %end;
    %end;
  run;
%mend vars;
 
%vars(your_dataset_name)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 18:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444208#M111169</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-09T18:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444211#M111171</link>
      <description>&lt;P&gt;Thanks novinosrin, I found that article &lt;A href="http://support.sas.com/kb/22/218.html" target="_self"&gt;here&lt;/A&gt;, but my question is more aimed at why my code is not functioning. Shouldn't the "put" function change the type from numeric?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 18:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444211#M111171</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-03-09T18:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444213#M111173</link>
      <description>&lt;P&gt;Back to basic,you need new assignment and can;t change existing mate i.e you need a new array&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input ID YEAR Manager $  TURNOVER;
  drop TURNOVER;
datalines;
1 1990 a 0
1 1991 a 0
1 1992 b 1
1 1993 b 0
1 1994 b 0
1 1995 c 1
1 1996 c 0
1 1997 c 0
2 1993 d 0
2 1994 d 0
2 1995 d 0
2 1996 e 1
2 1997 e 0
2 1998 e 0
2 1999 e 0
2 2000 e 0
2 2001 e 0
;
run;



data convertnums;
	set sashelp.class; 
	array all_n[*] _numeric_ ;
	array t(3) $ _age _height _weight;
	do i = 1 to dim(all_n); 
		t[i] = strip(put(all_n[i], 8.));  
	end;
run; 
proc contents data = convertnums;   * Check to see if numeric type converted ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 18:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444213#M111173</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-09T18:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444215#M111174</link>
      <description>&lt;P&gt;at the time of compilation, sas reads the descriptive portion of the dataset and builds the PDV. Therefore, this rule will prevail.&lt;/P&gt;&lt;P&gt;Execution: merely deals with data portion&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 19:01:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444215#M111174</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-09T19:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444216#M111175</link>
      <description>&lt;P&gt;Since your using the same variable names and when you have SET then formats are defined for those variables so, SAS automatically converts the data to numeric.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would do something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select STRIP(name)||"="||STRIP(name)||"_",STRIP(name)||"_",name INTO: rename separated by " ",:drop separated by " ",:need separated by " "
	from dictionary.columns
		where libname="SASHELP" and MEMNAME="CLASS"
	and type="num";
quit;
%put &amp;amp;need;

data convertnums(drop=&amp;amp;drop.);
/*format &amp;amp;need $8.;*/
	set sashelp.class(rename=(&amp;amp;rename.)); 
	array all_n[*] _numeric_ ;
	array need_Vars[*] $ &amp;amp;need.;
	
	do i = 1 to dim(all_n); 
		need_Vars[i] = strip(put(all_n[i], 8.));  * Not working... decode; 
	end;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 20:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444216#M111175</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-09T20:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444229#M111177</link>
      <description>&lt;P&gt;this might help you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into : NN1-: NN&amp;amp;sysmaxlong FROM DICTIONARY.COLUMNS where libname="SASHELP" and MEMNAME="ASSCMGR" and Type="num";&lt;BR /&gt;%let last=&amp;amp;sqlobs;&lt;BR /&gt;quit;&lt;BR /&gt;Options mprint mlogic merror;&lt;/P&gt;&lt;P&gt;%macro Z;&lt;BR /&gt;data want;&lt;BR /&gt;set sashelp.ASSCMGR;&lt;BR /&gt;%do I=1 %to &amp;amp;last;&lt;BR /&gt;&amp;amp;&amp;amp;NN&amp;amp;i.._New=put(&amp;amp;&amp;amp;NN&amp;amp;i,Best32.);&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%z;&lt;/P&gt;&lt;P&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 19:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444229#M111177</guid>
      <dc:creator>Bhushan</dc:creator>
      <dc:date>2018-03-09T19:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444231#M111178</link>
      <description>&lt;P&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select name into : NN1-: NN&amp;amp;sysmaxlong FROM DICTIONARY.COLUMNS where libname="SASHELP" and MEMNAME="ASSCMGR" and Type="num";&lt;BR /&gt;%let last=&amp;amp;sqlobs;&lt;BR /&gt;quit;&lt;BR /&gt;Options mprint mlogic merror;&lt;/P&gt;&lt;P&gt;%macro Z;&lt;BR /&gt;data want;&lt;BR /&gt;set sashelp.ASSCMGR;&lt;BR /&gt;%do I=1 %to &amp;amp;last;&lt;BR /&gt;&amp;amp;&amp;amp;NN&amp;amp;i.._New=put(&amp;amp;&amp;amp;NN&amp;amp;i,Best32.);&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%z;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 19:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444231#M111178</guid>
      <dc:creator>Bhushan</dc:creator>
      <dc:date>2018-03-09T19:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric variables to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444295#M111204</link>
      <description>&lt;P&gt;One easy way is to use PROC TRANSPOSE twice.&lt;/P&gt;
&lt;P&gt;Two things you need to make sure you have.&lt;/P&gt;
&lt;P&gt;1) A way to uniquely identify the original rows.&lt;/P&gt;
&lt;P&gt;2) At least one character variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a little test dataset that has a number and a date variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  _row_+1;
  input name :$20. age date :date9. ;
  format date date9. ;
cards;
Sam 20 01JAN2018
Julie 30 30DEC2016
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in that dataset _ROW_ is my unique identifier.&amp;nbsp; So now let's transpose it.&amp;nbsp; Notice we will need to exclude _ROW_ from the resulting data.&amp;nbsp; Then transpose it back.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=test
   out=middle (where=(lowcase(_name_) ne '_row_'))
;
  by _row_;
  var _all_;
run;

proc transpose data=middle out=want (drop=_name_);
  by _row_;
  var col1 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's see what happened.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=want; run;
proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;The CONTENTS Procedure

Alphabetic List of Variables and Attributes

#    Variable    Type    Len

1    _row_       Num       8
3    age         Char     20
4    date        Char     20
2    name        Char     20
&lt;/PRE&gt;
&lt;PRE&gt;Obs    _row_    name     age      date

 1       1      Sam      20     01JAN2018
 2       2      Julie    30     30DEC2016
&lt;/PRE&gt;
&lt;P&gt;If you don't know if you have an id variable or a character variable then you could just create one first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data to_transpose ;
  _row_ + 1;
  length _charvar_ $12 ;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then use that as the source table to start with.&amp;nbsp; You can later drop the _ROW_ and _CHARVAR_ variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that it will typically right align the converted numeric variables. If you would rather have them left aligned add a step between the proc transpose steps to modify the "middle" dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data middle;
  set middle;
  col1=left(col1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 22:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-variables-to-character/m-p/444295#M111204</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-09T22:48:03Z</dc:date>
    </item>
  </channel>
</rss>

