<?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 numerical categories of a variable to character data in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513788#M2584</link>
    <description>&lt;P&gt;I thought that using a hash-object could be an easy way to solve the problem, totally forgetting that hash-objects are something beginners should not torture theirs minds with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code uses the example-datasets, kindly supplied by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select cats('str_', Name, '=', Name)
      into :renameList separated by ' '
      from sashelp.vcolumn
         where LibName = 'WORK' and MemName = 'CHR1' and Name like 'rs%'
   ;

   select cats('str_', Name)
      into :nameList separated by ' '
      from sashelp.vcolumn
         where LibName = 'WORK' and MemName = 'CHR1' and Name like 'rs%'
   ;
quit;


data want;
   if 0 then set snp;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.snp');
      h.defineKey('snp', 'variant');
      h.defineData('code');
      h.defineDone();
   end;

   set chr1;
   length &amp;amp;nameList. $ 2;

   array old rs:;
   array new str_:;

   do i = 1 to dim(old);
      Snp = vname(old[i]);
      variant = old[i];

      if h.find() = 0 then do;
         new[i] = Code;
      end;
      else do;
         new[i] = .;
         put 'WARNING: No Code found';
         put _all_;
      end;
   end; 

   drop rs: i snp variant code;
   rename &amp;amp;renameList.;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 16 Nov 2018 06:21:24 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2018-11-16T06:21:24Z</dc:date>
    <item>
      <title>Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513665#M2565</link>
      <description>&lt;P&gt;I have a file in which rows represent individual IDs and columns are genotype data for a given genetic variant. Below is a sample of the structure, but the overall data is ~300K IDs x ~400 variants.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to recode the "0, 1, 2" numerical categories for any given variant (column) into corresponding genotypes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I need to convert the 0, 1, 2&amp;nbsp;data of the column variable "rs12044597_G" into AA, GA, GG, where 0 = AA, 1 = GA and 2 = GG.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;rs12044597_G&lt;/TD&gt;&lt;TD&gt;rs12711521_A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data chr1_recode;&lt;BR /&gt;set chr1;&lt;/P&gt;&lt;P&gt;rs12044597 = .;&lt;/P&gt;&lt;P&gt;if (rs12044597_G =0) then rs12044597 = "AA";&lt;/P&gt;&lt;P&gt;if (rs12044597_G =1) then rs12044597 = "GA";&lt;/P&gt;&lt;P&gt;if (rs12044597_G =2) then rs12044597 = "GG";&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This&amp;nbsp;does not work and results in large #s of errors, that look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasNote"&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;76:56 76:101 76:146&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;NOTE: Invalid numeric data, 'GA' , at line 76 column 101.&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;I realize that I am trying to convert numeric to character data, which is likely causing the issue. I'm familiar with the PUT statement for converting single data points from numeric to character, but how can this be incorporated into an if-then statement to achieve the data outcome I need, which should look like this:&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;rs12044597_G&lt;/TD&gt;&lt;TD&gt;rs12711521_A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;GA&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;I'm using SAS University Edition.&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;Thanks!&lt;/DIV&gt;</description>
      <pubDate>Thu, 15 Nov 2018 21:44:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513665#M2565</guid>
      <dc:creator>rwwalker</dc:creator>
      <dc:date>2018-11-15T21:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513674#M2566</link>
      <description>&lt;P&gt;&lt;STRONG&gt;rs12044597 = .;&amp;nbsp; --&amp;gt; &lt;/STRONG&gt;Remove this in your code. Your giving a value period which is missing numeric values and then later your giving a character value, that column is already defined as numeric and won't take character values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value cat 0='AA'
		  1='GA'
		  2='GG'
		  ;
run;

data chr1_recode;
format rs12044597_G rs12711521_A cat.;
set chr1;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 21:58:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513674#M2566</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-15T21:58:33Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513682#M2567</link>
      <description>&lt;P&gt;Do you have a table/dataset listing the correspondance between numerical categories and genotypes for each genetic variant? If so, that table could easily be converted to formats (one for each variant).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other way to proceed is to switch to a long data format (Variables: ID variant genotype) and join that table with&amp;nbsp;the correspondance&amp;nbsp;table.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 22:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513682#M2567</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-15T22:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513686#M2568</link>
      <description>&lt;P&gt;PGStats, I just generated a table. Sample below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;snp&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs12044597_G&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;TD&gt;GA&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs12711521_A&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;AC&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs3820071_A&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;TD&gt;AG&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs3766160_A&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;TD&gt;AG&lt;/TD&gt;&lt;TD&gt;AA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs10799790_T&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;TC&lt;/TD&gt;&lt;TD&gt;TT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs3765407_T&lt;/TD&gt;&lt;TD&gt;GG&lt;/TD&gt;&lt;TD&gt;TG&lt;/TD&gt;&lt;TD&gt;TT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rs477830_T&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;TC&lt;/TD&gt;&lt;TD&gt;TT&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would code look like to&amp;nbsp;get me where I need to go? Keep in mind this is for ~500 variants across almost 400,000 ids&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 22:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513686#M2568</guid>
      <dc:creator>rwwalker</dc:creator>
      <dc:date>2018-11-15T22:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513691#M2570</link>
      <description>What is the SNP in this context? The variable that this would map to? What do you mean by 500 variants? The number of records doesn't really matter here.</description>
      <pubDate>Thu, 15 Nov 2018 22:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513691#M2570</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-15T22:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513696#M2571</link>
      <description>&lt;P&gt;The join method would&amp;nbsp;go like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data chr1;
input ID rs12044597_G rs12711521_A ;
datalines;
1 1 2 
2 0 2 
3 2 2 
;

data snp;
input snp :$12. @;
do variant = 0 to 2;
    input code :$2. @;
    output;
    end;
datalines; 
rs12044597_G AA GA GG 
rs12711521_A CC AC AA 
rs3820071_A GG AG AA 
rs3766160_A GG AG AA 
rs10799790_T CC TC TT 
rs3765407_T GG TG TT 
rs477830_T CC TC TT 
;

proc transpose data=chr1 out=chrLong prefix=chr name=genotype;
by id;
var rs: ;
run;

proc sql;
create table chrLongCodes as
select 
    ID,
    genotype,
    code
from chrLong as a inner join snp on snp=genotype and chr1=variant
order by id, genotype;
quit;

proc transpose data=chrLongCodes out=chrCodes(drop=_name_);
by id;
id genotype;
var code;
run;

proc print data=chrCodes noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Nov 2018 22:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513696#M2571</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-15T22:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513788#M2584</link>
      <description>&lt;P&gt;I thought that using a hash-object could be an easy way to solve the problem, totally forgetting that hash-objects are something beginners should not torture theirs minds with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code uses the example-datasets, kindly supplied by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select cats('str_', Name, '=', Name)
      into :renameList separated by ' '
      from sashelp.vcolumn
         where LibName = 'WORK' and MemName = 'CHR1' and Name like 'rs%'
   ;

   select cats('str_', Name)
      into :nameList separated by ' '
      from sashelp.vcolumn
         where LibName = 'WORK' and MemName = 'CHR1' and Name like 'rs%'
   ;
quit;


data want;
   if 0 then set snp;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.snp');
      h.defineKey('snp', 'variant');
      h.defineData('code');
      h.defineDone();
   end;

   set chr1;
   length &amp;amp;nameList. $ 2;

   array old rs:;
   array new str_:;

   do i = 1 to dim(old);
      Snp = vname(old[i]);
      variant = old[i];

      if h.find() = 0 then do;
         new[i] = Code;
      end;
      else do;
         new[i] = .;
         put 'WARNING: No Code found';
         put _all_;
      end;
   end; 

   drop rs: i snp variant code;
   rename &amp;amp;renameList.;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Nov 2018 06:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/513788#M2584</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-11-16T06:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Convert numerical categories of a variable to character data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/514497#M2753</link>
      <description>&lt;P&gt;Thanks, I'll give this a shot&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 17:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-numerical-categories-of-a-variable-to-character-data/m-p/514497#M2753</guid>
      <dc:creator>rwwalker</dc:creator>
      <dc:date>2018-11-19T17:02:41Z</dc:date>
    </item>
  </channel>
</rss>

