<?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: New Variable in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750650#M10044</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if age = 45-49 then agecat = 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This statement does a subtraction (that's what SAS thinks when it sees the character –), and tests to see if age is equal to the subtraction 45 minus 49 which is –4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think what you want to do is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if age&amp;lt;=49 and age&amp;gt;=45 then agecat=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An equivalent version of the above, which is less typing is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 45&amp;lt;=age&amp;lt;=49 then agecat=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, for most analyses, you don't really need to create a new variable anyway. Using formats allows most SAS procedure to produce the value of 1 whenever you have ages between 45 and 49:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value agef 45-49 = '1' 50-54 = '2' ... /* I didn't type the rest*/ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then you assign the format agef to your variable age in a data step or PROC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format age agef.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why use the relatively meaningless value of 1 to indicate an age group????? This is a poor idea. Make your decisions so the output is more readable and meaningful. This code make the text string '45-49' appear wherever the ages are in that group and also allows most SAS procedures to operate on the group of ages between 45-49 as a single category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value agef 45-49 = '45-49' 50-54 = '50-54' ... /* I didn't type the rest*/ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, no new variable needed to create these age categories, and a more readable result can be obtained as well. The whole idea of creating categories named 1, 2, 3 ... really doesn't make sense here; categories named '45-49' etc make a whole lot more sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is a homework assignment, then I consider it to be a very poor assignment, as creating a new variable which has a value of 1 for the first age group is not a good thing to be encouraging; using formats would be a much better approach.&lt;/P&gt;</description>
    <pubDate>Sun, 27 Jun 2021 11:35:24 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-06-27T11:35:24Z</dc:date>
    <item>
      <title>New Variable</title>
      <link>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750632#M10042</link>
      <description>&lt;P&gt;Apologies in advance, I am very new to SAS and am having trouble with an assignment.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm getting error 161-185 no matching DO/SELECT statement. The assignment is described below:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Within the datastep create the following new variables:&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(a) A new variable called agecat that divides age into 5 categories:45-49;50-54;55-59;60-&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;64;65-69. Give values 1-5 for the categories.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(b) A new variable called income40 equal to 0 if income is less than $40,000 and equal to&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;1 if income is $40,000 or more.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(c) A new variable called collgrad equal to 0 if the participant did not graduate from college&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;and equal to 1 if the participant graduated from college (see study forms for income and&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;education categories)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(d) A new variable that is the lowest of the two serum potassium values&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my SAS code right now (still haven't gotten to parts C and D):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;libname t '/home/u58860515/';&lt;BR /&gt;data tdata; set t.tomhs (keep=age sex income educ hdlbl potassbl potass12);&lt;BR /&gt;if age = 45-49 then agecat = 1; else&lt;BR /&gt;if age = 50-54 then agecat = 2; else&lt;BR /&gt;if age = 55-59 then agecat = 3; else&lt;BR /&gt;if age = 60-64 then agecat = 4; else&lt;BR /&gt;if age = 65-69 then agecat = 5; end;&lt;BR /&gt;if income &amp;lt; 40000 then income40 = 0; else&lt;BR /&gt;if income &amp;gt; 40000 then income40 = 1; end;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 03:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750632#M10042</guid>
      <dc:creator>sassypixie</dc:creator>
      <dc:date>2021-06-27T03:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: New Variable</title>
      <link>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750637#M10043</link>
      <description>&lt;P&gt;You're well on your way to solving this.&amp;nbsp; Just get rid of the "end;" statement in both places.&amp;nbsp; Then continue creating the remaining variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"end;" is only needed when there is a DO or SELECT statement, as the error message indicates.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 04:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750637#M10043</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-06-27T04:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: New Variable</title>
      <link>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750650#M10044</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if age = 45-49 then agecat = 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This statement does a subtraction (that's what SAS thinks when it sees the character –), and tests to see if age is equal to the subtraction 45 minus 49 which is –4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think what you want to do is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if age&amp;lt;=49 and age&amp;gt;=45 then agecat=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An equivalent version of the above, which is less typing is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 45&amp;lt;=age&amp;lt;=49 then agecat=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, for most analyses, you don't really need to create a new variable anyway. Using formats allows most SAS procedure to produce the value of 1 whenever you have ages between 45 and 49:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value agef 45-49 = '1' 50-54 = '2' ... /* I didn't type the rest*/ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then you assign the format agef to your variable age in a data step or PROC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format age agef.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why use the relatively meaningless value of 1 to indicate an age group????? This is a poor idea. Make your decisions so the output is more readable and meaningful. This code make the text string '45-49' appear wherever the ages are in that group and also allows most SAS procedures to operate on the group of ages between 45-49 as a single category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value agef 45-49 = '45-49' 50-54 = '50-54' ... /* I didn't type the rest*/ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, no new variable needed to create these age categories, and a more readable result can be obtained as well. The whole idea of creating categories named 1, 2, 3 ... really doesn't make sense here; categories named '45-49' etc make a whole lot more sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is a homework assignment, then I consider it to be a very poor assignment, as creating a new variable which has a value of 1 for the first age group is not a good thing to be encouraging; using formats would be a much better approach.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 11:35:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750650#M10044</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-27T11:35:24Z</dc:date>
    </item>
    <item>
      <title>Re: New Variable</title>
      <link>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750654#M10045</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;There are many approaches to solving your issue.&lt;BR /&gt;I would prefer by creating formats and using them. With your case in mind, I would have the following code to create formats&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
	value **bleep**e low-45='LOW' 45-49=1 50-54=2 55-59=3 60-64=4 65-69=5 69-high='High';
	value fincome Low - &amp;lt; 40000=0 40000 - high=1;
	value $grad "N"=0 "Y"=1 Other="Missing";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Points to note.&lt;BR /&gt;If age is less then 45 or missing, you will get a "LOW". Similarly for age greater than 69 you will see high.&lt;/P&gt;
&lt;P&gt;For income less than 40000 includes missing values.&lt;BR /&gt;I have assumed that for college graduation your data has a response of 'Y' or 'N". Accordingly the values are set.&lt;BR /&gt;If this value is missing you will&amp;nbsp; "Missing".&lt;/P&gt;
&lt;P&gt;You need to code based on your detailed logic and data.&lt;/P&gt;
&lt;P&gt;For somebody&amp;nbsp; new to SAS, I would recommend this training from SAS&amp;nbsp;&lt;A href="https://support.sas.com/edu/schedules.html?crs=PROG1&amp;amp;ctry=US" target="_blank"&gt;https://support.sas.com/edu/schedules.html?crs=PROG1&amp;amp;ctry=US&lt;/A&gt;&amp;nbsp;. This gives a good understanding of SAS.&lt;/P&gt;
&lt;P&gt;Having a copy of at least one of the books&amp;nbsp; "&lt;STRONG&gt;The SAS Little SAS Book&lt;/STRONG&gt;" or "&lt;STRONG&gt;SAS Programming By Example&lt;/STRONG&gt;" is very helpful and saves time.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 11:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/New-Variable/m-p/750654#M10045</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-27T11:22:57Z</dc:date>
    </item>
  </channel>
</rss>

