<?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: MACROS in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255024#M6914</link>
    <description>&lt;P&gt;Thank you for your help. Now that I have that done is there a macro in which i ould be able to minimize repeating the same step but with different datasets. As shown below after the macro is created the same macor is applied to 3 different datasets. I would like to further minimize repetition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro try;&lt;/P&gt;&lt;P&gt;if newrace= '4' then newrace2 = '1. Black';&lt;BR /&gt;if newrace= '1' then newrace2= '2. Hispanic';&lt;BR /&gt;if newrace= '6' then newrace2= '3. White';&lt;BR /&gt;if newrace in ('3', '5', '7', '2', '8', '9', ' ') then newrace2= '4. API &amp;amp;Other';&lt;/P&gt;&lt;P&gt;if 0 le livingage&amp;amp;year. le 24 then livingage_new&amp;amp;year.='0-24';&lt;BR /&gt;if 25 le livingage&amp;amp;year. le 34 then livingage_new&amp;amp;year.='25-34';&lt;BR /&gt;if 35 le livingage&amp;amp;year. le 44 then livingage_new&amp;amp;year.='35-44';&lt;BR /&gt;if 45 le livingage&amp;amp;year. le 54 then livingage_new&amp;amp;year.='45-54';&lt;BR /&gt;if 55 le livingage&amp;amp;year. then livingage_new&amp;amp;year.='55+';&lt;BR /&gt;%mend try;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data estab0_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set estab1_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data estab1_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set estab1_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data nr_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set nr_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Mar 2016 17:45:25 GMT</pubDate>
    <dc:creator>beverlyobeng</dc:creator>
    <dc:date>2016-03-07T17:45:25Z</dc:date>
    <item>
      <title>MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255010#M6908</link>
      <description>&lt;P&gt;I would greatly appreciate it if someone could help me solve my current dilemma. I woulld like to create a macro that can be used in several datasteps. Below is what I have done so far. I keep getting error messages that the new variables (&lt;SPAN&gt;newrace2 and&amp;nbsp;&lt;/SPAN&gt;are &lt;SPAN&gt;livingage_new&amp;amp;year.)&amp;nbsp;&lt;/SPAN&gt;unitialized.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro try;&lt;BR /&gt;%if newrace= '4' %then %let newrace2='1. Black';&lt;BR /&gt;%if newrace= '1' %then %let newrace2='2. Hispanic';&lt;BR /&gt;%if newrace= '6' %then %let newrace2='3. White';&lt;BR /&gt;%if newrace= '3' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '5' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '7' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '2' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '8' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '9' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= ' ' %then %let newrace2='4. API &amp;amp;Other';&lt;/P&gt;&lt;P&gt;%if 0 le livingage&amp;amp;year. le 24 %then %let livingage_new&amp;amp;year. ='0-24';&lt;BR /&gt;%if 25 le livingage&amp;amp;year. le 34 %then %let livingage_new&amp;amp;year. ='25-34';&lt;BR /&gt;%if 35 le livingage&amp;amp;year. le 44 %then %let livingage_new&amp;amp;year. ='35-44';&lt;BR /&gt;%if 45 le livingage&amp;amp;year. le 54 %then %let livingage_new&amp;amp;year. ='45-54';&lt;BR /&gt;%if 55 le livingage&amp;amp;year. %then %let livingage_new&amp;amp;year. ='55+';&lt;BR /&gt;run;&lt;BR /&gt;%mend try;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data estab0_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set estab1_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 17:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255010#M6908</guid>
      <dc:creator>beverlyobeng</dc:creator>
      <dc:date>2016-03-07T17:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255013#M6909</link>
      <description>&lt;P&gt;Your statements should probably be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if newrace= '4' then newrace2='1. Black';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;etc. %if, %then, and %let statements control the functioning of the macro, not the underlying SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, if you use some "else" clauses, things will be a little clearer. There is also a "select" statement that you could consider.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 17:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255013#M6909</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2016-03-07T17:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255017#M6911</link>
      <description>&lt;P&gt;Well, first off you don't seem to understand macro language. &amp;nbsp;Think of it this way, if you copy the the code in the macro and replace the call with it:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data estab0_demo2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;length newrace2 $13.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;length livingage_new&amp;amp;year $13.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set estab1_demo;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;%if newrace= '4' %then %let newrace2='1. Black';&lt;BR /&gt;%if newrace= '1' %then %let newrace2='2. Hispanic';&lt;BR /&gt;%if newrace= '6' %then %let newrace2='3. White';&lt;BR /&gt;%if newrace= '3' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '5' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '7' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '2' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '8' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= '9' %then %let newrace2='4. API &amp;amp;Other';&lt;BR /&gt;%if newrace= ' ' %then %let newrace2='4. API &amp;amp;Other';&lt;/P&gt;
&lt;P&gt;%if 0 le livingage&amp;amp;year. le 24 %then %let livingage_new&amp;amp;year. ='0-24';&lt;BR /&gt;%if 25 le livingage&amp;amp;year. le 34 %then %let livingage_new&amp;amp;year. ='25-34';&lt;BR /&gt;%if 35 le livingage&amp;amp;year. le 44 %then %let livingage_new&amp;amp;year. ='35-44';&lt;BR /&gt;%if 45 le livingage&amp;amp;year. le 54 %then %let livingage_new&amp;amp;year. ='45-54';&lt;BR /&gt;%if 55 le livingage&amp;amp;year. %then %let livingage_new&amp;amp;year. ='55+';&lt;BR /&gt;run;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Does the above look like valid Base SAS code? &amp;nbsp;No. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The second point I would ask is why? &amp;nbsp;What you are doing is coding what is essentially formats. &amp;nbsp;e.g.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;proc format;
  value $newrace
    '4' = '1. Black'
    ...;
run;

data want;  
  set have;
  format race $newrace.;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Please see the help docs:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000063536.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000063536.htm&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Alternatively, if you have to do it in code, then create a dataset with your code values and decodes values:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;CODE &amp;nbsp; &amp;nbsp;DECODE&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1. Black&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Then merge that data onto your other data, far easier to maintain.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 17:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255017#M6911</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-07T17:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255020#M6912</link>
      <description>THanks</description>
      <pubDate>Mon, 07 Mar 2016 17:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255020#M6912</guid>
      <dc:creator>beverlyobeng</dc:creator>
      <dc:date>2016-03-07T17:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255021#M6913</link>
      <description>Thank you</description>
      <pubDate>Mon, 07 Mar 2016 17:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255021#M6913</guid>
      <dc:creator>beverlyobeng</dc:creator>
      <dc:date>2016-03-07T17:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255024#M6914</link>
      <description>&lt;P&gt;Thank you for your help. Now that I have that done is there a macro in which i ould be able to minimize repeating the same step but with different datasets. As shown below after the macro is created the same macor is applied to 3 different datasets. I would like to further minimize repetition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro try;&lt;/P&gt;&lt;P&gt;if newrace= '4' then newrace2 = '1. Black';&lt;BR /&gt;if newrace= '1' then newrace2= '2. Hispanic';&lt;BR /&gt;if newrace= '6' then newrace2= '3. White';&lt;BR /&gt;if newrace in ('3', '5', '7', '2', '8', '9', ' ') then newrace2= '4. API &amp;amp;Other';&lt;/P&gt;&lt;P&gt;if 0 le livingage&amp;amp;year. le 24 then livingage_new&amp;amp;year.='0-24';&lt;BR /&gt;if 25 le livingage&amp;amp;year. le 34 then livingage_new&amp;amp;year.='25-34';&lt;BR /&gt;if 35 le livingage&amp;amp;year. le 44 then livingage_new&amp;amp;year.='35-44';&lt;BR /&gt;if 45 le livingage&amp;amp;year. le 54 then livingage_new&amp;amp;year.='45-54';&lt;BR /&gt;if 55 le livingage&amp;amp;year. then livingage_new&amp;amp;year.='55+';&lt;BR /&gt;%mend try;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data estab0_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set estab1_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data estab1_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set estab1_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data nr_demo2;&lt;BR /&gt;length newrace2 $13.;&lt;BR /&gt;length livingage_new&amp;amp;year $13.;&lt;BR /&gt;set nr_demo;&lt;BR /&gt;%try;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 17:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255024#M6914</guid>
      <dc:creator>beverlyobeng</dc:creator>
      <dc:date>2016-03-07T17:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255026#M6915</link>
      <description>&lt;P&gt;I'll second&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;recommendation of using a format instead. I'll also suggest using a double format for the race variable so it sorts without the extra 1, 2 in the labels. If you need help to code that let me know.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A a good reference for formats is the paper titled Proc Format: Not Just Another Pretty Face&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 17:48:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255026#M6915</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-07T17:48:50Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255035#M6916</link>
      <description>&lt;P&gt;This code is actually the closest you have come to getting something working.&amp;nbsp; I would stick with the approach, but there is a key question to answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you expect &amp;amp;YEAR to be assigned a value?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 18:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255035#M6916</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-03-07T18:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: MACROS</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255040#M6917</link>
      <description>&lt;P&gt;To shorten it even more, take a look in the help documentation about how to pass parameters into macros. If you pass the "estab0_demo" type information as a parameter, you could make the whole thing into a macro. Some similar examples are in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm" target="_self"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, the advice about using formats is excellent. You should investigate those as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 18:15:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/MACROS/m-p/255040#M6917</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2016-03-07T18:15:48Z</dc:date>
    </item>
  </channel>
</rss>

