<?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: Creating one single new variable from multiple variables using IF statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600992#M76359</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/216793"&gt;@Cindarellie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello!&lt;BR /&gt;I'm trying to create a variable that just pools all of the participants responses into one variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, there was a question that ask which following fruits do you eat? The participants can check off whether or not they eat each option, where 1=they checked it. So I want to create a variable where all the yes responses are in just one if that make senses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this code, I don't get any errors and the variable is created, however I noticed through PROC FREQ the frequency doesn't match the original variables-so I am wondering how to make sure it is pulling things correctly or what is happening for it to be getting the wrong number of observations?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure creating the variable using the if function is possible as that is most familiar method I use to create new variables. I am not familiar with arrays but would would that be better?&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;if q_apple=1 then&lt;BR /&gt;fruits="apple";&lt;BR /&gt;else if q_orange=1 then&lt;BR /&gt;fruits="orange";&lt;BR /&gt;else if q_banana=1 then&lt;BR /&gt;fruits="banana";&lt;/P&gt;
&lt;P&gt;else if q_grapes=1 then&lt;BR /&gt;fruits="grapes";&lt;/P&gt;
&lt;P&gt;else if q_watermelon=1 then&lt;BR /&gt;fruits="watermelon";&lt;/P&gt;
&lt;P&gt;else if q_strawberries=1 then&lt;BR /&gt;fruits="strawberries";&lt;/P&gt;
&lt;P&gt;else if q_bb=1 then&lt;BR /&gt;fruits="blueberries";&lt;/P&gt;
&lt;P&gt;else if q_ch=1 then&lt;BR /&gt;fruits="cherries";&lt;BR /&gt;else if q_other=1 then&lt;BR /&gt;fruits="Other";&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show example starting data and what the result should look like. Otherwise there is lots of guessing as to what you want.&lt;/P&gt;</description>
    <pubDate>Fri, 01 Nov 2019 15:11:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-11-01T15:11:54Z</dc:date>
    <item>
      <title>Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600952#M76351</link>
      <description>&lt;P&gt;Hello!&lt;BR /&gt;I'm trying to create a variable that just pools all of the participants responses into one variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, there was a question that ask which following fruits do you eat? The participants can check off whether or not they eat each option, where 1=they checked it. So I want to create a variable where all the yes responses are in just one if that make senses.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this code, I don't get any errors and the variable is created, however I noticed through PROC FREQ the frequency doesn't match the original variables-so I am wondering how to make sure it is pulling things correctly or what is happening for it to be getting the wrong number of observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure creating the variable using the if function is possible as that is most familiar method I use to create new variables. I am not familiar with arrays but would would that be better?&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;if q_apple=1 then&lt;BR /&gt;fruits="apple";&lt;BR /&gt;else if q_orange=1 then&lt;BR /&gt;fruits="orange";&lt;BR /&gt;else if q_banana=1 then&lt;BR /&gt;fruits="banana";&lt;/P&gt;&lt;P&gt;else if q_grapes=1 then&lt;BR /&gt;fruits="grapes";&lt;/P&gt;&lt;P&gt;else if q_watermelon=1 then&lt;BR /&gt;fruits="watermelon";&lt;/P&gt;&lt;P&gt;else if q_strawberries=1 then&lt;BR /&gt;fruits="strawberries";&lt;/P&gt;&lt;P&gt;else if q_bb=1 then&lt;BR /&gt;fruits="blueberries";&lt;/P&gt;&lt;P&gt;else if q_ch=1 then&lt;BR /&gt;fruits="cherries";&lt;BR /&gt;else if q_other=1 then&lt;BR /&gt;fruits="Other";&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 13:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600952#M76351</guid>
      <dc:creator>Cindarellie</dc:creator>
      <dc:date>2019-11-01T13:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600959#M76352</link>
      <description>&lt;P&gt;What if you have more than one fruit checked by a single person?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 13:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600959#M76352</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-01T13:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600964#M76354</link>
      <description>?I'm sorry I meant to say that the frequency changes&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 01 Nov 2019 13:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600964#M76354</guid>
      <dc:creator>Cindarellie</dc:creator>
      <dc:date>2019-11-01T13:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600967#M76355</link>
      <description>&lt;P&gt;If the output of the &lt;FONT face="courier new,courier"&gt;proc freq&lt;/FONT&gt; is showing fewer observations then that might be because you have some missing values. The number of missing values might be displayed after all other frequencies, depending upon how you have chosen to write the &lt;FONT face="courier new,courier"&gt;proc freq&lt;/FONT&gt; code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be helpful if you provided a complete sample data step with sample data that can be created like&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener"&gt;this&lt;/A&gt;&amp;nbsp;and the &lt;FONT face="courier new,courier"&gt;proc freq&lt;/FONT&gt; code you have used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 13:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600967#M76355</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-11-01T13:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600975#M76356</link>
      <description>&lt;P&gt;you might want to look at catx function&amp;nbsp;&lt;FONT style="background-color: #ffffff;"&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257076.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257076.htm&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;and create fruits&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 14:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600975#M76356</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-11-01T14:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600983#M76357</link>
      <description>&lt;P&gt;If you want PROC FREQ to be able to count everything, your data set needs to contain everything.&amp;nbsp; Look at this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if q_apple=1 then
fruits="apple";
else if q_orange=1 then
fruits="orange";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It can only assign one value to FRUITS.&amp;nbsp; If more than one item has been checked, the program won't pick it up.&amp;nbsp; It ends up with APPLE or ORANGE, but not both.&amp;nbsp; Here's the sort of change you need to make before running PROC FREQ:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if q_apple=1 then do;
   fruits="apple";
   output;
end;
if q_orange=1 then do;
   fruits="orange";  
   output;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way, every selection becomes part of the data that feeds into PROC FREQ.&amp;nbsp; Do NOT use the word ELSE.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 14:52:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600983#M76357</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-11-01T14:52:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600989#M76358</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length fruits $256;
set have;
 
if q_apple=1 then
fruits=catx(" ", fruits, "apple");
if q_orange=1 then
fruits=catx(" ", fruits, "orange");
if q_banana=1 then
fruits=catx(" ", fruits, "banana");
if q_grapes=1 then
fruits=catx(" ", fruits, "grapes");
if q_watermelon=1 then
fruits=catx(" ", fruits, "watermelon");
if q_strawberries=1 then
fruits=catx(" ", fruits, "strawberries");
if q_bb=1 then
fruits=catx(" ", fruits, "blueberries");
if q_ch=1 then
fruits=catx(" ", fruits, "cherries");
if q_other=1 then
fruits=catx(" ", fruits, "Other");
run;
proc freq data=want;
tables fruits;
quit;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 15:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600989#M76358</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-11-01T15:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating one single new variable from multiple variables using IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600992#M76359</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/216793"&gt;@Cindarellie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello!&lt;BR /&gt;I'm trying to create a variable that just pools all of the participants responses into one variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, there was a question that ask which following fruits do you eat? The participants can check off whether or not they eat each option, where 1=they checked it. So I want to create a variable where all the yes responses are in just one if that make senses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this code, I don't get any errors and the variable is created, however I noticed through PROC FREQ the frequency doesn't match the original variables-so I am wondering how to make sure it is pulling things correctly or what is happening for it to be getting the wrong number of observations?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure creating the variable using the if function is possible as that is most familiar method I use to create new variables. I am not familiar with arrays but would would that be better?&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;if q_apple=1 then&lt;BR /&gt;fruits="apple";&lt;BR /&gt;else if q_orange=1 then&lt;BR /&gt;fruits="orange";&lt;BR /&gt;else if q_banana=1 then&lt;BR /&gt;fruits="banana";&lt;/P&gt;
&lt;P&gt;else if q_grapes=1 then&lt;BR /&gt;fruits="grapes";&lt;/P&gt;
&lt;P&gt;else if q_watermelon=1 then&lt;BR /&gt;fruits="watermelon";&lt;/P&gt;
&lt;P&gt;else if q_strawberries=1 then&lt;BR /&gt;fruits="strawberries";&lt;/P&gt;
&lt;P&gt;else if q_bb=1 then&lt;BR /&gt;fruits="blueberries";&lt;/P&gt;
&lt;P&gt;else if q_ch=1 then&lt;BR /&gt;fruits="cherries";&lt;BR /&gt;else if q_other=1 then&lt;BR /&gt;fruits="Other";&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show example starting data and what the result should look like. Otherwise there is lots of guessing as to what you want.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 15:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-one-single-new-variable-from-multiple-variables-using/m-p/600992#M76359</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-01T15:11:54Z</dc:date>
    </item>
  </channel>
</rss>

