<?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: converting char data into numeric in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797879#M33077</link>
    <description>&lt;P&gt;If you want to create dummies SAS knows all about that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transreg data=sashelp.class design;
   model class(sex/zero=none) class(age/zero=none);
   id _all_;
   output out=coded(drop=Intercept);
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 603px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68747i7DD501BF71EA534D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 20:11:32 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2022-02-22T20:11:32Z</dc:date>
    <item>
      <title>converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797877#M33076</link>
      <description>&lt;P&gt;Greetings&lt;/P&gt;
&lt;P&gt;I have a data that contains several non-metric variables. based on each of these variable I need to create new variables with a value of either 0 or 1.&lt;/P&gt;
&lt;P&gt;for example if the variable is gender which may have either male or female, I'm trying to create a column says gender1 which has 1 when ever male 0 otherwise and gender2 which has a value of 1 if female 0 otherwise.&lt;/P&gt;
&lt;P&gt;I wrote the following code and I'm facing a problem with the if statement. illustrated below&amp;nbsp;&lt;/P&gt;
&lt;P&gt;______________________________________________________&lt;/P&gt;
&lt;P&gt;ods html close;&lt;BR /&gt;ods html;&lt;/P&gt;
&lt;P&gt;proc import datafile="C:\Users\123\Desktop\training\DATA.csv"&lt;BR /&gt;out=numaricFROSHV1&lt;BR /&gt;dbms=csv&lt;BR /&gt;replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;/*count all columns in all the data sets in the given library*/&lt;/P&gt;
&lt;P&gt;select left(put(count(*),8.))&amp;nbsp;&lt;BR /&gt;into :numds&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1';&lt;BR /&gt;select name &lt;BR /&gt;into :cl1 - :cl&amp;amp;numds &lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1'; &lt;BR /&gt;quit;&lt;BR /&gt;%MACRO DO_BRANCH;&lt;BR /&gt;%do i=1 %to &amp;amp;numds;&lt;BR /&gt;proc sql;&lt;BR /&gt;select left(put(count(distinct &amp;amp;&amp;amp;cl&amp;amp;i),8.))&amp;nbsp;&lt;BR /&gt;into :dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct &amp;amp;&amp;amp;cl&amp;amp;i /*name here is the column name*/&lt;BR /&gt;into :vald1 - :vald&amp;amp;dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;data newvar;&lt;BR /&gt;set numaricFROSHV1&lt;BR /&gt;%do j=1 %to &amp;amp;dis;&lt;BR /&gt;%put &amp;amp;&amp;amp;cl&amp;amp;i=&amp;amp;&amp;amp;vald&amp;amp;j; &lt;/P&gt;
&lt;LI-SPOILER&gt;&lt;STRONG&gt;%if &amp;amp;&amp;amp;cl&amp;amp;i=&amp;amp;&amp;amp;vald&amp;amp;j %then &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=1;&lt;BR /&gt;/* if the value of &amp;amp;&amp;amp;vald&amp;amp;j is to large then there is an error and it seems that&amp;nbsp;&amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=1 is not the prober way of writing the new variable name*/&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;%else &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=0;&lt;/STRONG&gt;&lt;/LI-SPOILER&gt;
&lt;P&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%MEND DO_BRANCH;&lt;BR /&gt;%DO_BRANCH;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 19:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797877#M33076</guid>
      <dc:creator>Abdulla1</dc:creator>
      <dc:date>2022-02-22T19:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797879#M33077</link>
      <description>&lt;P&gt;If you want to create dummies SAS knows all about that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transreg data=sashelp.class design;
   model class(sex/zero=none) class(age/zero=none);
   id _all_;
   output out=coded(drop=Intercept);
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 603px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68747i7DD501BF71EA534D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797879#M33077</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2022-02-22T20:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797882#M33078</link>
      <description>&lt;P&gt;Interesting approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC GLMSELECT will actually do this for you and depending on which procedure you're using in SAS the CLASS statement may handle the dummy variable process - PROC LOGISTIC, PHREG support CLASS statement, as does GLM though with slightly less options. &lt;BR /&gt;Here is an example of how to use PROC GLMSELECT to do this dynamically, as well as links to a few other methods in the links at the bottom of the post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Variables/ta-p/308484" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Variables/ta-p/308484&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To answer your question asked - I think you're using macro logic when you should be using data step logic with macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may need to play around with the &amp;amp; to see how many are required to resolve them.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i="&amp;amp;&amp;amp;vald&amp;amp;j" then &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=1;
else &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/414636"&gt;@Abdulla1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Greetings&lt;/P&gt;
&lt;P&gt;I have a data that contains several non-metric variables. based on each of these variable I need to create new variables with a value of either 0 or 1.&lt;/P&gt;
&lt;P&gt;for example if the variable is gender which may have either male or female, I'm trying to create a column says gender1 which has 1 when ever male 0 otherwise and gender2 which has a value of 1 if female 0 otherwise.&lt;/P&gt;
&lt;P&gt;I wrote the following code and I'm facing a problem with the if statement. illustrated below&amp;nbsp;&lt;/P&gt;
&lt;P&gt;______________________________________________________&lt;/P&gt;
&lt;P&gt;ods html close;&lt;BR /&gt;ods html;&lt;/P&gt;
&lt;P&gt;proc import datafile="C:\Users\123\Desktop\training\DATA.csv"&lt;BR /&gt;out=numaricFROSHV1&lt;BR /&gt;dbms=csv&lt;BR /&gt;replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;/*count all columns in all the data sets in the given library*/&lt;/P&gt;
&lt;P&gt;select left(put(count(*),8.))&amp;nbsp;&lt;BR /&gt;into :numds&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1';&lt;BR /&gt;select name &lt;BR /&gt;into :cl1 - :cl&amp;amp;numds &lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1'; &lt;BR /&gt;quit;&lt;BR /&gt;%MACRO DO_BRANCH;&lt;BR /&gt;%do i=1 %to &amp;amp;numds;&lt;BR /&gt;proc sql;&lt;BR /&gt;select left(put(count(distinct &amp;amp;&amp;amp;cl&amp;amp;i),8.))&amp;nbsp;&lt;BR /&gt;into :dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct &amp;amp;&amp;amp;cl&amp;amp;i /*name here is the column name*/&lt;BR /&gt;into :vald1 - :vald&amp;amp;dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;data newvar;&lt;BR /&gt;set numaricFROSHV1&lt;BR /&gt;%do j=1 %to &amp;amp;dis;&lt;BR /&gt;%put &amp;amp;&amp;amp;cl&amp;amp;i=&amp;amp;&amp;amp;vald&amp;amp;j;&lt;/P&gt;
&lt;LI-SPOILER&gt;&lt;STRONG&gt;%if &amp;amp;&amp;amp;cl&amp;amp;i=&amp;amp;&amp;amp;vald&amp;amp;j %then &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=1;&lt;BR /&gt;/* if the value of &amp;amp;&amp;amp;vald&amp;amp;j is to large then there is an error and it seems that&amp;nbsp;&amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=1 is not the prober way of writing the new variable name*/&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;%else &amp;amp;&amp;amp;&amp;amp;cl&amp;amp;i&amp;amp;j=0;&lt;/STRONG&gt;&lt;/LI-SPOILER&gt;
&lt;P&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%MEND DO_BRANCH;&lt;BR /&gt;%DO_BRANCH;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797882#M33078</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-22T20:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797888#M33079</link>
      <description>&lt;P&gt;that saved me a lot of time.&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797888#M33079</guid>
      <dc:creator>Abdulla1</dc:creator>
      <dc:date>2022-02-22T20:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797891#M33080</link>
      <description>&lt;P&gt;thanks Reeza&lt;/P&gt;
&lt;P&gt;I'm planning to use proc logistic, and proc reg as well. I didn't know such option exist.&lt;/P&gt;
&lt;P&gt;that is helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:45:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797891#M33080</guid>
      <dc:creator>Abdulla1</dc:creator>
      <dc:date>2022-02-22T20:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797892#M33081</link>
      <description>For PROC LOGISTIC, you can add the variables to the CLASS statement then, none of this is needed. It is need for PROC REG unfortunately. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/797892#M33081</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-22T20:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: converting char data into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/799210#M33143</link>
      <description>&lt;P&gt;if anyone is still interested in using the code I hade originally to do this task then here is the corrected version of it that worked with me:&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;/*count all columns in all the data sets in the given library*/&lt;BR /&gt;select left(put(count(*),8.)) &lt;BR /&gt;into :numds&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1';&lt;BR /&gt;/*store all columns names as macro variables cl1 - cl&amp;amp;numds*/&lt;BR /&gt;select name &lt;BR /&gt;into :cl1 - :cl&amp;amp;numds &lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname='WORK' and memname='NUMARICFROSHV1'; &lt;BR /&gt;quit;&lt;BR /&gt;%MACRO DO_BRANCH;&lt;BR /&gt;%do i=1 %to &amp;amp;numds;&lt;BR /&gt;%let nana=&amp;amp;&amp;amp;cl&amp;amp;i;/*nana= variable header*/&lt;BR /&gt;/*count distinct values in each column*/&lt;BR /&gt;proc sql;&lt;BR /&gt;select left(put(count(distinct &amp;amp;nana),8.)) &lt;BR /&gt;into :dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;%put the value of &amp;amp;nana dis is &amp;amp;dis;&lt;BR /&gt;/*save each distinct values into a macro.var vald*/&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct &amp;amp;nana &lt;BR /&gt;into :vald1 - :vald&amp;amp;dis&lt;BR /&gt;from numaricFROSHV1;&lt;BR /&gt;quit;&lt;BR /&gt;data numaricFROSHV1;&lt;BR /&gt;set numaricFROSHV1;&lt;BR /&gt;%do j=1 %to &amp;amp;dis;&lt;BR /&gt;if &amp;amp;nana="&amp;amp;&amp;amp;vald&amp;amp;j" then &amp;amp;nana&amp;amp;j=1;&lt;BR /&gt;else &amp;amp;nana&amp;amp;j=0;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%MEND DO_BRANCH;&lt;BR /&gt;%DO_BRANCH;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 21:11:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/converting-char-data-into-numeric/m-p/799210#M33143</guid>
      <dc:creator>Abdulla1</dc:creator>
      <dc:date>2022-02-28T21:11:00Z</dc:date>
    </item>
  </channel>
</rss>

