<?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 How to convert specific coded categorical data to numerical data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541021#M149315</link>
    <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to standardize food questionaire scores in such a way that every level or category corresponds to number of times/ day in my data set. For example, i want number 0 in my dataset to be converted to 0, 1 to 1/30, 2 to 1/15, 3 to 1/7 etc.&lt;/P&gt;&lt;P&gt;I tried to write a SAS micro for it. Here's my code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro convert_var(var_in,var_out);&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 0 then &amp;amp;var_out. = 0;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 1 then &amp;amp;var_out. = 1/30;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 2 then &amp;amp;var_out. = 2/30;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 3 then &amp;amp;var_out. = 1/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 4 then &amp;amp;var_out. = 3/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 5 then &amp;amp;var_out. = 6/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 6 then &amp;amp;var_out. = 1;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 7 then &amp;amp;var_out. = 2.5;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 8 then &amp;amp;var_out. = 4.5;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 9 then &amp;amp;var_out. = 7;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;data work.fooddata;&lt;/P&gt;&lt;P&gt;%convert_var(T1,i_T1)&lt;/P&gt;&lt;P&gt;%convert_var(T2,i_T2)&lt;/P&gt;&lt;P&gt;%convert_var(T3,i_T3)&lt;/P&gt;&lt;P&gt;%convert_var(T4,i_T4)&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran this code for the first 4 variables that i wanted to convert and it gave me this as my dataset. Note the intial content (before i ran the code) and final content of my dataset(after i ran the code).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will be glad to get help on how to modify my code....or can this be done with a do loop? Please any help will be appreciated!&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pic 1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27731i6E36C65217FCDE7A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pic 1.PNG" alt="Pic 1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pic2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27732i12F3647F3EF069F0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pic2.JPG" alt="Pic2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2019 09:20:19 GMT</pubDate>
    <dc:creator>ChuksManuel</dc:creator>
    <dc:date>2019-03-07T09:20:19Z</dc:date>
    <item>
      <title>How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541021#M149315</link>
      <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to standardize food questionaire scores in such a way that every level or category corresponds to number of times/ day in my data set. For example, i want number 0 in my dataset to be converted to 0, 1 to 1/30, 2 to 1/15, 3 to 1/7 etc.&lt;/P&gt;&lt;P&gt;I tried to write a SAS micro for it. Here's my code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro convert_var(var_in,var_out);&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 0 then &amp;amp;var_out. = 0;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 1 then &amp;amp;var_out. = 1/30;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 2 then &amp;amp;var_out. = 2/30;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 3 then &amp;amp;var_out. = 1/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 4 then &amp;amp;var_out. = 3/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 5 then &amp;amp;var_out. = 6/7;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 6 then &amp;amp;var_out. = 1;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 7 then &amp;amp;var_out. = 2.5;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 8 then &amp;amp;var_out. = 4.5;&lt;/P&gt;&lt;P&gt;if &amp;amp;var_in. = 9 then &amp;amp;var_out. = 7;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;data work.fooddata;&lt;/P&gt;&lt;P&gt;%convert_var(T1,i_T1)&lt;/P&gt;&lt;P&gt;%convert_var(T2,i_T2)&lt;/P&gt;&lt;P&gt;%convert_var(T3,i_T3)&lt;/P&gt;&lt;P&gt;%convert_var(T4,i_T4)&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran this code for the first 4 variables that i wanted to convert and it gave me this as my dataset. Note the intial content (before i ran the code) and final content of my dataset(after i ran the code).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will be glad to get help on how to modify my code....or can this be done with a do loop? Please any help will be appreciated!&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pic 1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27731i6E36C65217FCDE7A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pic 1.PNG" alt="Pic 1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pic2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27732i12F3647F3EF069F0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pic2.JPG" alt="Pic2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 09:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541021#M149315</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-03-07T09:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541022#M149316</link>
      <description>&lt;P&gt;The answer is yes, a do loop would be better, something like (not tested as no test data in the form of a datastep):&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array vals{10} t1--vc2;
  array res{10} 8.;
  do i=1 to 10;
    select(vals{i});
      when(1) res=0;
      when(2) res=1/30;
      ...
      otherwise;
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Or use a format applied to the variables, that would be simpler:&lt;/P&gt;
&lt;PRE&gt;proc format;
  val tmp 
    1=0
    2=0.0333
    ...;
run;

data want;
  set have;
  format t1 t2 ... vc2 tmp.;
run;&lt;/PRE&gt;
&lt;P&gt;And even better than that would be to normalise your data, so have data going down rather than across, e.g.:&lt;/P&gt;
&lt;PRE&gt;visitdata  paramcd aval
42291      T1      2
42291      T2      0&lt;/PRE&gt;
&lt;P&gt;In this way you only have one variable to alter - and of course you would be following industry guidelines (CDISC) in your data structure.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 09:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541022#M149316</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-07T09:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541024#M149318</link>
      <description>&lt;P&gt;How about a picture format?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format library=work;
   picture MyFmt (default=8)
      0 = '0'
      1 = '1/30'
      2 = '2/30'
      3 = '1/7'
      4 = '3/7'
      5 = '6/7'
      6 = '1'
      7 = '2.5'
      8 = '4.5'
      9 = '7'
      ;
run;

data Somedata;
   do x=1 to 9;
      output;
   end;

   format x MyFmt.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Mar 2019 09:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541024#M149318</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-07T09:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541025#M149319</link>
      <description>&lt;P&gt;Start with Maxim 2: Read the Log. If that does not give you a clue, post the log here, using the {i} button to preserve it's contents and formatting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, provide your example data in usable form. That means a data step with datalines, posted into a window opened with the "little running man" icon. My footnotes provide links to useful resources, especially a macro that can convert existing datasets into such code, so we can recreate your dataset with ALL atttributes &lt;U&gt;as is&lt;/U&gt; by simply copy/pasting the code and submitting it.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 09:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541025#M149319</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-07T09:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541034#M149327</link>
      <description>None of the solutions worked. Thank you for trying guys i will keep working on it.</description>
      <pubDate>Thu, 07 Mar 2019 10:32:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541034#M149327</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-03-07T10:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541036#M149329</link>
      <description>&lt;P&gt;This doesn't tell us anything.&amp;nbsp; "Not worked" could mean anything, didn't run, didn't do what you expected, there was a power cut, aliens invaded???&amp;nbsp; We can only advise on the information provided here.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 10:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541036#M149329</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-07T10:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541041#M149331</link>
      <description>&lt;P&gt;Thank you. It did not run.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="pic3.JPG" style="width: 577px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27733i2D521D2023C455B4/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic3.JPG" alt="pic3.JPG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="pic4.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27734iE467DCC2B3FA884B/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic4.JPG" alt="pic4.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 10:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541041#M149331</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-03-07T10:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541046#M149332</link>
      <description>&lt;P&gt;So, in the future, we want log files pasted into the window that appears after you click on the {i} icon. DO NOT SKIP THIS STEP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before your PROC FORMAT, it says &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;The SAS System stopped processing this step because of errors&lt;/STRONG&gt;&lt;/FONT&gt;. So we need the LOG before the PROC FORMAT you show, please paste it into the window that appears after you click on the {i} icon. DO NOT SKIP THIS STEP.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 11:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541046#M149332</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-03-07T11:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert specific coded categorical data to numerical data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541048#M149334</link>
      <description>&lt;P&gt;PLEASE USE THE {i} BUTTON FOR POSTING LOGS. Copy/paste the text into the subwindow. No need to go through the hassle of making screenshots, posting them, and end up with hard-to-read pics.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS looks for a &lt;EM&gt;character&lt;/EM&gt; format, which means that at least one of your variables is of type character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clean out your code from the top down, so fix the ERROR in the previous proc format.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 11:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-specific-coded-categorical-data-to-numerical-data/m-p/541048#M149334</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-07T11:44:05Z</dc:date>
    </item>
  </channel>
</rss>

