<?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: Create new variables with condition in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467449#M70695</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/101951"&gt;@kgrover&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to run this code, but for some reason "sum" is not in blue&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data merged11; set merged10;&lt;BR /&gt;array num{*} CIINTERF--CIPROBLM;&lt;BR /&gt;if sum(of num(*)) &amp;gt;= 2 then tobdodr = 1;&lt;BR /&gt;else tobdodr = 0;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is something wrong with my code?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The syntax highlighter does not automatically highlight most actual numeric functions an a data step. Possibly this decision on the part of the SAS designers is because so many people routinely create variables such as sum, count, min, max and who knows what all. So don't let non-highlighted functions worry you too much. If you misspell one such as summ( of ...) then you may get a message&lt;/P&gt;
&lt;PRE&gt;ERROR 68-185: The function SUMM is unknown, or cannot be accessed.
&lt;/PRE&gt;
&lt;P&gt;with underscores indicating where you used the misspelled function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Jun 2018 15:56:05 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-06-04T15:56:05Z</dc:date>
    <item>
      <title>Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466912#M70647</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have about 14 variables in total and if &lt;STRONG&gt;2 or more&lt;/STRONG&gt; of those variables are answered "Yes", I need to create a new variable to define a disorder.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the past, I have used if then statements to define a new variable but because there are so many possibilities, with several combinations,&amp;nbsp;I was wondering if there is a faster way of coding this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 16:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466912#M70647</guid>
      <dc:creator>kgrover</dc:creator>
      <dc:date>2018-06-01T16:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466916#M70649</link>
      <description>How are your variables stored? There’s a few options, one is to concatenate all the results and use COUNTW to count the number of YES or NO. &lt;BR /&gt;&lt;BR /&gt;X = catx(“ “, of var1-var14);&lt;BR /&gt;N_Yes = countw(x, ‘Yes’);&lt;BR /&gt;</description>
      <pubDate>Fri, 01 Jun 2018 16:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466916#M70649</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-01T16:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466931#M70651</link>
      <description>&lt;P&gt;All the variables are stored in one data set&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not familiar with this SAS code, can you please elaborate what is to be indicated in the " "? does this code get used with proc print?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;X = catx(“ “, of var1-var14);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N_Yes = countw(x, ‘Yes’);&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 16:56:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466931#M70651</guid>
      <dc:creator>kgrover</dc:creator>
      <dc:date>2018-06-01T16:56:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466937#M70652</link>
      <description>&lt;P&gt;Nothing is to be added, this runs in a data step, not PROC PRINT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Concatenate&amp;nbsp;all the responses into a single variable separated by a space&lt;/P&gt;
&lt;P&gt;CATX(“ “ , of var1-var14) ;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;X will look like: No Yes No Yes No No NO etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Counts the number of times Yes is in the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COUNTW(X, “Yes”);&lt;/P&gt;
&lt;P&gt;Should return 2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Please check the documentation on the functions to see what they do in more details.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/101951"&gt;@kgrover&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;All the variables are stored in one data set&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not familiar with this SAS code, can you please elaborate what is to be indicated in the " "? does this code get used with proc print?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;X = catx(“ “, of var1-var14);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N_Yes = countw(x, ‘Yes’);&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&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>Fri, 01 Jun 2018 17:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466937#M70652</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-01T17:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466938#M70653</link>
      <description>&lt;P&gt;Actually you should provide some example data. "Yes" could be coded a moderately large number of ways. Also are all of your variables involved the same type: all&amp;nbsp;numeric or all&amp;nbsp;character? All coded the same?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 17:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466938#M70653</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-01T17:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466948#M70654</link>
      <description>&lt;P&gt;They are all numeric, I have attached a screenshot of the variables&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 17:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466948#M70654</guid>
      <dc:creator>kgrover</dc:creator>
      <dc:date>2018-06-01T17:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466987#M70659</link>
      <description>&lt;P&gt;If you have all numeric values which are to be included for comparison then simply use arrays with _numeric_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ ABC BCD CDE DEF;
datalines ;
A 1 0 0 0 0
B 0 1 1 1 0
C 0 0 0 0 0
D 0 1 1 1 0
;
run;

data want;
set have;
array num{*} _numeric_;
if sum(of num(*))&amp;gt;2 then new_var="Yes";
else new_var="No";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For suppose if you have extra numeric variable something like ID that needs be ignored in sum function then you need to define the variables in array. Double dash(--) would be useful to specify all variables by simply typing the first and last variables names in the order they exist in dataset.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID ABC BCD CDE DEF;
datalines ;
101 1 0 0 0 0
102 0 1 1 1 0
103 0 0 0 0 0
104 0 1 1 1 0
;
run;

data want;
set have;
array num{*} ABC--DEF /* Order in which they exist in dataset*/ ;
if sum(of num(*))&amp;gt;2 then new_var="Yes";
else new_var="No";
run;&lt;/CODE&gt;&lt;/PRE&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>Fri, 01 Jun 2018 18:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/466987#M70659</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-06-01T18:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467052#M70664</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The OP is asking for "2 or more", so instead of &amp;gt; 2 you would use&lt;/P&gt;
&lt;PRE&gt;if sum(of num(*))&amp;gt; GE then new_var="Yes";&lt;/PRE&gt;
&lt;P&gt;Though I would probably use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;newvar= ( sum(of num(*)) ge 2 );&lt;/PRE&gt;
&lt;P&gt;to get a numeric dichotomous 1/0 coded variable. The data description provided indicates that should not be unfamiliar with this project and it is easy to get certain summary statistics (sum is number of Yes, mean is percent yes)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 23:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467052#M70664</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-01T23:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467437#M70694</link>
      <description>&lt;P&gt;Thank you so much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to run this code, but for some reason "sum" is not in blue&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merged11; set merged10;&lt;BR /&gt;array num{*} CIINTERF--CIPROBLM;&lt;BR /&gt;if sum(of num(*)) &amp;gt;= 2 then tobdodr = 1;&lt;BR /&gt;else tobdodr = 0;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is something wrong with my code?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 15:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467437#M70694</guid>
      <dc:creator>kgrover</dc:creator>
      <dc:date>2018-06-04T15:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467449#M70695</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/101951"&gt;@kgrover&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to run this code, but for some reason "sum" is not in blue&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data merged11; set merged10;&lt;BR /&gt;array num{*} CIINTERF--CIPROBLM;&lt;BR /&gt;if sum(of num(*)) &amp;gt;= 2 then tobdodr = 1;&lt;BR /&gt;else tobdodr = 0;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is something wrong with my code?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The syntax highlighter does not automatically highlight most actual numeric functions an a data step. Possibly this decision on the part of the SAS designers is because so many people routinely create variables such as sum, count, min, max and who knows what all. So don't let non-highlighted functions worry you too much. If you misspell one such as summ( of ...) then you may get a message&lt;/P&gt;
&lt;PRE&gt;ERROR 68-185: The function SUMM is unknown, or cannot be accessed.
&lt;/PRE&gt;
&lt;P&gt;with underscores indicating where you used the misspelled function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 15:56:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variables-with-condition/m-p/467449#M70695</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-04T15:56:05Z</dc:date>
    </item>
  </channel>
</rss>

