<?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 to create binary variable from existing categorical variable acording to frquency of categor in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395270#M95279</link>
    <description>&lt;P&gt;You probably have the original data set open in a data viewer (EG? Check Tools-&amp;gt;View Open Data Sets.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid, you can work from a copy by sorting the original into another file and work forward from there -- or else just make sure all data grid views are closed before running the next step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* to create new cluster status variable*/
proc freq data=genotyped_1210;
tables clustername / out=counts (keep=clustername count);
run;
 
/*Next, sort your original data set:*/
 
Proc sort data= genotyped_1210 out=sorted_genotyped;
by clustername;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Sep 2017 20:07:40 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2017-09-12T20:07:40Z</dc:date>
    <item>
      <title>How to create binary variable from existing categorical variable acording to frquency of categories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395206#M95266</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a new user of SAS 9.4 program and I&amp;nbsp;am working with a data of my research. I have this character column which looks like:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Var1&amp;nbsp;&lt;/P&gt;&lt;P&gt;XY_001&lt;/P&gt;&lt;P&gt;XY_021&lt;/P&gt;&lt;P&gt;XY_001&amp;nbsp;&lt;/P&gt;&lt;P&gt;XY_000&lt;/P&gt;&lt;P&gt;XY_015 .......etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it is around&amp;nbsp;633 obsrvations and have also 577 missings! All observations are 1210&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and if I run a proc freq for this variable the frequencies of the readings are different ranging from 1 to 38 and&amp;nbsp; the missings&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I want to create a new variable called Var2 I want to make it binary (0/1). for those categories who have 1 as a frequency to be 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;and those categories who have frequency more or equal than 2 to be 1. and the missings as missing&lt;/P&gt;&lt;P&gt;and to call 0: not group&amp;nbsp;&lt;/P&gt;&lt;P&gt;and 1:&amp;nbsp;group&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I create the program??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 18:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395206#M95266</guid>
      <dc:creator>alamoubm</dc:creator>
      <dc:date>2017-09-12T18:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395222#M95270</link>
      <description>&lt;P&gt;Let's start by extending the PROC FREQ that you ran, so it also creates an output data set:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc freq data=have;
 tables var1 / out=counts (keep=var1 count);
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, sort your original data set:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sort data=have;
 by var1;
run;

&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, merge the PROC FREQ results with your data:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data want;
merge have
    &amp;nbsp; counts (where=(var1 &amp;gt; ' '));
by var1;
if count=1 then flag=0;
else if count &amp;gt; 1 then flag=1;
drop count;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 12:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395222#M95270</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-03-10T12:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395251#M95277</link>
      <description>&lt;P&gt;Hi Astounding,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply I have an issue:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I run this code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* to create new cluster status variable*/
proc freq data=genotyped_1210;
tables clustername / out=counts (keep=clustername count);
run;
&amp;nbsp;
/*Next, sort your original data set:*/
&amp;nbsp;
Proc sort data= genotyped_1210;
by clustername;
run;
&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The 1st part wet well, the sorting part gave me this error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;80&amp;nbsp;&amp;nbsp; Proc sort data= genotyped_1210;&lt;BR /&gt;81&amp;nbsp;&amp;nbsp; by clustername;&lt;BR /&gt;82&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;ERROR: You cannot open WORK.GENOTYPED_1210.DATA for output access with member-level control because&lt;BR /&gt;WORK.GENOTYPED_1210.DATA is in use by you in resource environment SORT.&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SORT used (Total process time):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do you suggest?&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 19:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395251#M95277</guid>
      <dc:creator>alamoubm</dc:creator>
      <dc:date>2017-09-12T19:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395270#M95279</link>
      <description>&lt;P&gt;You probably have the original data set open in a data viewer (EG? Check Tools-&amp;gt;View Open Data Sets.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid, you can work from a copy by sorting the original into another file and work forward from there -- or else just make sure all data grid views are closed before running the next step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* to create new cluster status variable*/
proc freq data=genotyped_1210;
tables clustername / out=counts (keep=clustername count);
run;
 
/*Next, sort your original data set:*/
 
Proc sort data= genotyped_1210 out=sorted_genotyped;
by clustername;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Sep 2017 20:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395270#M95279</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2017-09-12T20:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395294#M95291</link>
      <description>&lt;P&gt;Thank you ChrisHemedinger,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I closed all the data viewer and it worked!&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when doing the next program as follows&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Finally, merge the PROC FREQ results with your data:*/
&amp;nbsp;
data TB;
merge sorted_genotyped
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counts (where=(clustername &amp;gt; ' '));
by clustername;
if count=1 then unique=0;
else if count &amp;gt; 1 then cluster=1;
drop count;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The following error was:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;58&amp;nbsp;&amp;nbsp; data TB;&lt;BR /&gt;59&amp;nbsp;&amp;nbsp; merge sorted_genotyped&lt;BR /&gt;60&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counts (where=(clustername &amp;gt; ' '));&lt;BR /&gt;ERROR: The value &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; is not a valid SAS name.&lt;BR /&gt;61&amp;nbsp;&amp;nbsp; by clustername;&lt;BR /&gt;62&amp;nbsp;&amp;nbsp; if count=1 then unique=0;&lt;BR /&gt;63&amp;nbsp;&amp;nbsp; else if count &amp;gt; 1 then cluster=1;&lt;BR /&gt;64&amp;nbsp;&amp;nbsp; drop count;&lt;BR /&gt;65&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.TB may be incomplete.&amp;nbsp; When this step was stopped there were 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; observations and 239 variables.&lt;BR /&gt;WARNING: Data set WORK.TB was not replaced because this step was stopped.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;attached picture of example of the character values of the variable: clustername&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="issue1.jpg" style="width: 377px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15073iB5E1CBADF06223EB/image-size/large?v=v2&amp;amp;px=999" role="button" title="issue1.jpg" alt="issue1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 20:40:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395294#M95291</guid>
      <dc:creator>alamoubm</dc:creator>
      <dc:date>2017-09-12T20:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395322#M95310</link>
      <description>&lt;P&gt;This won't explain the error, but it is a small required fix. &amp;nbsp;You now have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; if count=1 then unique=0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; else if count &amp;gt; 1 then cluster=1;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That needs to become:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&amp;nbsp; if count=1 then unique=0;&lt;BR /&gt;&amp;nbsp; else if count &amp;gt; 1 then unique=1;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The error message itself it not really explainable. &amp;nbsp;You can try changing the WHERE condition from this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;(where=(clustername &amp;gt; ' '))&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;to this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;(where=(clustername ne ' '))&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also make sure the quotes around the blank space are really quotes ... you can try deleting them, then typing them back in again. &amp;nbsp;It shouldn't make a difference, but you never know.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 21:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/395322#M95310</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-12T21:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/396865#M95858</link>
      <description>&lt;P&gt;Thanks all for your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem still didn't solve it could be because I have an ID numbers in the original file thatcouldaffect the merge step ?!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will try to use R instead to see if this could solve the problem!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciatr all your suggestions and programs you provided I tried them all and it didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2017 16:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/396865#M95858</guid>
      <dc:creator>alamoubm</dc:creator>
      <dc:date>2017-09-18T16:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/396900#M95878</link>
      <description>&lt;P&gt;What is it that you want as your output?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  length Var1 $10;
  input Var1 @@ ;
cards;  
XY_001 XY_021 . XY_001 . XY_000 . XY_015 .
;

proc sql noprint ;
  create table want as 
    select var1,count(*) as nobs
         , (calculated nobs &amp;gt; 1) as var2
    from have
    group by 1
  ;
quit;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs     Var1     nobs    var2

 1                 4       1
 2     XY_000      1       0
 3     XY_001      2       1
 4     XY_015      1       0
 5     XY_021      1       0
&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Sep 2017 18:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/396900#M95878</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-18T18:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/397132#M95953</link>
      <description>&lt;P&gt;Thank you for your all contributions the problem is solved by R!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2017 14:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/397132#M95953</guid>
      <dc:creator>alamoubm</dc:creator>
      <dc:date>2017-09-19T14:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create binary variable from existing categorical variable acording to frquency of categor</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/801176#M315287</link>
      <description>Hi,&lt;BR /&gt;i have three categorical variables in my data set:&lt;BR /&gt;1. alcohol (current=1, other=0)&lt;BR /&gt;2. smk (current=1, other=0)&lt;BR /&gt;3.drug (current=1, other=0)&lt;BR /&gt;now i want to creat a variable that only includes level1 of these variables:&lt;BR /&gt;here is what I wrote:&lt;BR /&gt;if alcohol=1 &amp;amp; smk=1 &amp;amp; drug=1 then (new variable name)= 1; else (new variable)=0;&lt;BR /&gt;however this is not showing in my proc freq.&lt;BR /&gt;any advice is appreciated.&lt;BR /&gt;Thanks</description>
      <pubDate>Wed, 09 Mar 2022 20:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-binary-variable-from-existing-categorical-variable/m-p/801176#M315287</guid>
      <dc:creator>raheleh22</dc:creator>
      <dc:date>2022-03-09T20:03:29Z</dc:date>
    </item>
  </channel>
</rss>

