<?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 a new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744060#M233045</link>
    <description>1. Remove comma in your numbers&lt;BR /&gt;2. Use ELSE IF rather than just IF (THIS IS IMPORTANT for the next issue)&lt;BR /&gt;3. Does your third category/condition make sense? I don't think it matches your condition above and will overwrite all your previous values except anything over 200k.&lt;BR /&gt;4. Check the length of your SalesRange variable. It will likely get truncated and you may want to explicitly set the length. SAS defaults the length to the first assigned value.</description>
    <pubDate>Wed, 26 May 2021 22:28:37 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-05-26T22:28:37Z</dc:date>
    <item>
      <title>Creating a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744055#M233042</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I am trying to use the sashelp.shoes dataset as an input in order to create an output dataset. Additionally, I have to create a new character variable SalesRange, which has the following 3 categories:-&lt;/P&gt;&lt;P&gt;-Lower when Sales are less than $100,000.&lt;BR /&gt;-Middle when Sales are between $100,000 and $200,000, inclusively.&lt;BR /&gt;-Upper when Sales are above $200,000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Produced below is my code. However, after different combinations of the if then else statements, I keep getting an error saying - Expecting an arithmetic operator and The symbol is not recognized and will be ignored. Any help is much appreciated. Thanks in advance!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.shoerange;&lt;BR /&gt;set sashelp.shoes;&lt;BR /&gt;if Sales&amp;lt;100,000 then SalesRange="Lower";&lt;BR /&gt;if 100,000&amp;lt;=Sales&amp;lt;=200,000 then SalesRange="Middle";&lt;BR /&gt;if Sales&amp;lt;200,000 then SalesRange="Upper";&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 21:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744055#M233042</guid>
      <dc:creator>shridula95</dc:creator>
      <dc:date>2021-05-26T21:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744058#M233043</link>
      <description>&lt;P&gt;You have a comma in the middle of the IF statements.&amp;nbsp; For example in the first IF it is separating the number 100 from the number zero.&lt;/P&gt;
&lt;PRE&gt;621   data work.shoerange;
622   set sashelp.shoes;
623   if Sales&amp;lt;100,000 then SalesRange="Lower";
                  -
                  388
                  200
&lt;/PRE&gt;
&lt;P&gt;Remove the commas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How long should the new SALESRANGE variable be?&lt;/P&gt;
&lt;P&gt;How long have you defined it to be?&lt;/P&gt;
&lt;P&gt;What will happen when you try to assign the string 'Middle' to SALESRANGE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 22:12:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744058#M233043</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-26T22:12:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744060#M233045</link>
      <description>1. Remove comma in your numbers&lt;BR /&gt;2. Use ELSE IF rather than just IF (THIS IS IMPORTANT for the next issue)&lt;BR /&gt;3. Does your third category/condition make sense? I don't think it matches your condition above and will overwrite all your previous values except anything over 200k.&lt;BR /&gt;4. Check the length of your SalesRange variable. It will likely get truncated and you may want to explicitly set the length. SAS defaults the length to the first assigned value.</description>
      <pubDate>Wed, 26 May 2021 22:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744060#M233045</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-26T22:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744092#M233058</link>
      <description>&lt;P&gt;Using the suggestions by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, your code can be rewritten like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.shoerange;
  set sashelp.shoes;
  length SalesRange $8;
  if sales&amp;lt;100000 then SalesRange='Lower';
  else if sales&amp;lt;=200000 then SalesRange='Middle';
  else SalesRange='Upper';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can create a format and use that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format lib=work;
  value SalesRange
    low-&amp;lt;100000='Lower'
    100000-200000='Middle'
    other='Upper'
    ;
run;

data work.shoerange;
  set sashelp.shoes;
  SalesRange=put(sales,SalesRange8.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can just use the format directly in e.g. PROC PRINT or PROC FREQ without calculating the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the real world, you may want to add a category for missing values. And you may want to save your format in another library than WORK.&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 06:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744092#M233058</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-05-27T06:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744114#M233066</link>
      <description>&lt;P&gt;Proc sql can also be use as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select *,
case 
when sales &amp;lt; 100000 then "Lower"
when sales between 100000 and 200000 then "Middle"
when sales &amp;gt; 200000 then "Upper"
end as SalesRange
from sashelp.shoes;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 May 2021 11:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable/m-p/744114#M233066</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-05-27T11:20:52Z</dc:date>
    </item>
  </channel>
</rss>

