<?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: recode a variable within a data step using macro variable conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377941#M90787</link>
    <description>&lt;P&gt;If your question is answered, please mark your answer as the solution and close the thread.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jul 2017 21:15:09 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-07-20T21:15:09Z</dc:date>
    <item>
      <title>recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377891#M90769</link>
      <description>&lt;P&gt;How can I combine my macro variable conditions to recode a variable in a data step? I try not to use macro variables when part of the data step but even when I change the code below to %do %if/%then outside of the dataset I run into errors, plus&amp;nbsp;I'd rather run one data step rather than a data step for each value of a.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a &amp;nbsp;list of variable names in list a and a list of variable names in list b. I need to do a loop within a loop to check that the names in list a = names in list b. If they are equal then I want to recode the variable name in list a to equal the element in a different list, b2 that is associated with list b.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let a = gender, age&amp;nbsp;&amp;nbsp;; &amp;nbsp;/*(in this example, yes, commas)*/&lt;/P&gt;&lt;P&gt;%let a_n = 2&amp;nbsp;;&lt;/P&gt;&lt;P&gt;%let b= age height weight&amp;nbsp;gender ;&lt;/P&gt;&lt;P&gt;%let b_n = 4;&lt;/P&gt;&lt;P&gt;%let b2= "unk1" "unk2" "unk3" &amp;nbsp;"unk4"; &amp;nbsp;/*(in my example they are quoted)*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data want ;&lt;/P&gt;&lt;P&gt;Data have ;&lt;/P&gt;&lt;P&gt;do x = 1 to &amp;amp;a_n. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do y=1 to &amp;amp;b_n. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if scan(&amp;amp;a, &amp;amp;x, ',') = scan(&amp;amp;b., &amp;amp;y.) then do ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if scan(&amp;amp;a,&amp;amp;x,' ') = '' then scan(&amp;amp;a,&amp;amp;x)= scan(&amp;amp;b2,&amp;amp;y.) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end ;&lt;/P&gt;&lt;P&gt;end ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the above to resolve to :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want ;&lt;/P&gt;&lt;P&gt;data have ;&lt;/P&gt;&lt;P&gt;if age&amp;nbsp;= '' then age="unk1" ;&lt;/P&gt;&lt;P&gt;if gender&amp;nbsp;= "" then gender="unk4" ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 18:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377891#M90769</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T18:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377894#M90770</link>
      <description>and the entire block of code is within a macro&lt;BR /&gt;</description>
      <pubDate>Thu, 20 Jul 2017 18:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377894#M90770</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T18:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377902#M90776</link>
      <description>&lt;P&gt;Can you use formats instead? If you have datasets with the data you can create the formats using a CNTLIN dataset and then you can dynamically build a FORMAT statement using SASHELP. VCOLUMNS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to use SASHELP anyways as it can tell you the variables in the data set and the formats your want to apply rather than the scan/check list process.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 19:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377902#M90776</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-20T19:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377904#M90777</link>
      <description>&lt;P&gt;This does not work either:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test() ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let a = gender, age&amp;nbsp;&amp;nbsp;; &amp;nbsp;/*(in this example, yes, commas)*/&lt;/P&gt;&lt;P&gt;%let a_n = 2&amp;nbsp;;&lt;/P&gt;&lt;P&gt;%let b= age height weight&amp;nbsp;gender ;&lt;/P&gt;&lt;P&gt;%let b_n = 4;&lt;/P&gt;&lt;P&gt;%let b2= "unk1" "unk2" "unk3" &amp;nbsp;"unk4"; &amp;nbsp;/*(in my example they are quoted)*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data want ;&lt;/P&gt;&lt;P&gt;Data have ;&lt;/P&gt;&lt;P&gt;array aa[&amp;amp;a_n.] &amp;nbsp;&amp;amp;a. ;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;array bb[&amp;amp;b_n.] &amp;nbsp;&amp;amp;b2. ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%do x = 1 %to &amp;amp;a_n. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %do y=1 %to &amp;amp;b_n. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%if %scan(&amp;amp;a, &amp;amp;x, ',') = %scan(&amp;amp;b., &amp;amp;y.) %then %do ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if aa[x]&amp;nbsp;= '' then aa[x]=bb[y];&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%end ;&lt;/P&gt;&lt;P&gt;%end ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%test () ;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 19:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377904#M90777</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T19:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377907#M90778</link>
      <description>&lt;P&gt;Yes, this can be done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As usual, you need to be warned that you should not be doing this.&amp;nbsp; You need to learn more SAS language first.&amp;nbsp; Your question illustrates this in a couple of ways.&amp;nbsp; You have this statement in several places:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That should actually be a SET statement.&amp;nbsp; So even if you got macro language to work, you would still have debugging to do.&amp;nbsp; And it's much harder to debug a program when you have a combination of macro language errors and SAS language errors.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, consider this statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if age='' then age="unk1";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This could only work properly if AGE is defined as a character variable, with a length of at least 4.&amp;nbsp; That would be a very unusual definition for a variable named AGE, but it is possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At any rate, here's a deal.&amp;nbsp; If you demonstrate sufficient macro language skills for this next task, I will finish the job later later today.&amp;nbsp; You have several %LET statements in your example.&amp;nbsp; Use those macro variables only, and use them inside a macro.&amp;nbsp; No DATA step code is necessary.&amp;nbsp; The macro code that you write must use the existing macro variables to generate the following macro statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%local age;&lt;/P&gt;
&lt;P&gt;%let age = "unk1";&lt;/P&gt;
&lt;P&gt;%local height;&lt;/P&gt;
&lt;P&gt;%let height = "unk2";&lt;/P&gt;
&lt;P&gt;%local weight;&lt;/P&gt;
&lt;P&gt;%let weight = "unk3";&lt;/P&gt;
&lt;P&gt;%local gender;&lt;/P&gt;
&lt;P&gt;%let gender = "unk4";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can program that, I will program the rest of it.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 20:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377907#M90778</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-20T20:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377910#M90780</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have; &amp;nbsp;&amp;nbsp;was a typo.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if age='' then age="unk1"; &amp;nbsp; - yes age is character field (age groups in alpha labels - not my code, part of a bigger project) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 20:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377910#M90780</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T20:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377915#M90782</link>
      <description>&lt;P&gt;Great.&amp;nbsp; So far so good.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 20:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377915#M90782</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-20T20:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377930#M90784</link>
      <description>&lt;P&gt;I have multiple datasets I am looping through with various assignments for my variable lists. The number of variables and the variable names will differ by dataset and by group variable (inner loop) - so many combinations of variable lists. It's important I, at least try, to stick to a list structure rather than name individual variables to a %let statement. My %let statements in my posts were to show you the macro variables I am dealing with as they relate to the code I am having trouble with.The actual lists were generated from proc sql into: or call symputs within the loops. I'll be more careful next time to explain the issues better. Thanks anyway.&amp;nbsp;&amp;nbsp;I figured it out.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377930#M90784</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T21:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377941#M90787</link>
      <description>&lt;P&gt;If your question is answered, please mark your answer as the solution and close the thread.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377941#M90787</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-20T21:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377943#M90788</link>
      <description>&lt;P&gt;Hi , Sorry for jumping in a bit too late. I have a feeling, you could make the solution less tedious and more effective by using Hashes or formats( as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;)suggested. I hope your solution is working, nevertheless the logic can be hard on the macro processor and the storage of large sets of variable lists in macro vars. Precisely, that invokes heavy macro processor work even before the datastep compiler is involved.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I haven't been active here lately however if I find time after my Final exams, I will try out the Hash solution and post it here. Anyways, if i missed out a point or two to read between the lines, kindly ignore my post. My apologies.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:24:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377943#M90788</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-07-20T21:24:15Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377944#M90789</link>
      <description>&lt;P&gt;If you don't need to the actual conversion, formats will be very efficient. Otherwise, even the ability to use PUTC/PUTN in combination of the format name would help. And formats are one of the fastest ways to recode data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377944#M90789</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-20T21:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377948#M90790</link>
      <description>&lt;P&gt;Thank you for your suggestions. I'm using the list variables in different ways so I'm not sure formats is the answer. I am familiar with hash and have used them in other projects as set table lookups but for this project proc sqls with dyanmically changing group by statements is all I need. Plus, it forces me to learn the macro facility!! I'm all set though so I don't want to take up your time. Good luck on your exams!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377948#M90790</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-20T21:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377950#M90792</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59791"&gt;@CP2&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;If using a macro for this then you've got so many different parameters to set that things become cumbersome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd go for a macro free approach. It doesn't hurt to have a few data steps with similar code blocks as this will still be much easier to read and maintain than implementing this via macro code.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377950#M90792</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-20T21:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377963#M90795</link>
      <description>&lt;P&gt;This type of problem is MUCH easier to do without macro code. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your "A" list is not used in your example of the generated code. &amp;nbsp;Perhaps you intended it to loop over a list of dataset? But for now let's just ignore it. &amp;nbsp;So your input metadata probably should look something like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data recodes;
  length varname $32 missing $32 ;
cards;
age unk1
height unk2
weight unk3
gender unk4
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then perhaps you have some list of tables that you want to transform&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsnlist=demo labs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you have some metadata about what variables on in which datasets. &amp;nbsp;So you could combine them and generate a list of variables that exist and need the transformation code written for them. &amp;nbsp;So the result might be a table like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_recodes;
  length member varname $32 missing $32 ;
  input member varname missing;
cards;
demog age unk1
demog height unk2
demog weight unk3
demog gender unk4
labs age unk1
labs height unk2
labs weight unk3
labs gender unk4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use that metadata table to generate the SAS program code you need to recode the missing values of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set ds_recodes ;
  by member ;
  file code ;
  if first.member then put
/ 'data ' member ';'
/ '  set ' member ';'
  ;
  put 
  '  if missing(' varname ') then ' varname '=' missing :$quote. ';'
  ;
  if last.member then put
  'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which for the data file above will generate code like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demog ;
  set demog ;
  if missing(age ) then age ="unk1" ;
  if missing(height ) then height ="unk2" ;
  if missing(weight ) then weight ="unk3" ;
  if missing(gender ) then gender ="unk4" ;
run;

data labs ;
  set labs ;
  if missing(age ) then age ="unk1" ;
  if missing(height ) then height ="unk2" ;
  if missing(weight ) then weight ="unk3" ;
  if missing(gender ) then gender ="unk4" ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can then execute by using a %INCLUDE statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include code / source2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 23:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/377963#M90795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-20T23:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/378072#M90825</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59791"&gt;@CP2&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;Plus, it forces me to learn the macro facility!&lt;/EM&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Sure thing! Use your chance to dig deeper into the SAS language. But don't use language structures only because it's fun for you (unless it's purely "academic").&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As for macro code: &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Very powerful but only use it if you "must" as it makes your&amp;nbsp;code easily "messy" and hard to read and maintain.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;May be some code like below is what you're after?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data recode_list;
  infile datalines dsd ;
  input _varname:$32. _val_orig:$20. _val_recode:$20.;
cards;
SEX,'F','X'
NAME,'ALFRED','Alf'
NAME,'WILLIAM','Wil'
NAME,'THOMAS','Tom'
;
run;

%macro recode(list);
  array char_vars {*} _character_;
  drop _:;
  if _n_=1 then 
    do;
      if 0 then set &amp;amp;list;
      dcl hash h1(dataset:"&amp;amp;list");
      h1.defineKey('_varname','_val_orig');
      h1.defineData('_val_recode');
      h1.defineDone();
    end;

  do _i=1 to dim(char_vars);
    if h1.find(key:upcase(vname(char_vars[_i])), key:upcase(char_vars[_i]))=0 then
      do;
        char_vars[_i]=strip(_val_recode);
      end;
  end;
%mend;

data test1;
  set sashelp.class;
  %recode(recode_list)
run;

data test2;
  set sashelp.classfit;
  %recode(recode_list)
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 11:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/378072#M90825</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-21T11:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: recode a variable within a data step using macro variable conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/378121#M90832</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59791"&gt;@CP2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As long as you are forcing yourself to learn macro language, here are some tools to consider. &amp;nbsp;I'll give you the short tour.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are starting with these macro variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let a = gender, age&amp;nbsp;&amp;nbsp;;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let b= age height weight&amp;nbsp;gender ;&lt;/P&gt;
&lt;P&gt;%let b2= "unk1" "unk2" "unk3" &amp;nbsp;"unk4"; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From that, you need to add calculations to the DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if age&amp;nbsp;= '' then age="unk1" ;&lt;/P&gt;
&lt;P&gt;if gender&amp;nbsp;= "" then gender="unk4" ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The programming was complex, but ultimately you found a solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you had started with a different set of macro variables, the task would be straightforward. &amp;nbsp;Starting with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let a = gender age&amp;nbsp;&amp;nbsp;;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let age = "unk1";&lt;/P&gt;
&lt;P&gt;%let height = "unk2";&lt;/P&gt;
&lt;P&gt;%let weight = "unk3";&lt;/P&gt;
&lt;P&gt;%let gender = "unk4";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From that starting point, the solution is easier:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;a));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%let varname = &amp;amp;scan(&amp;amp;a, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if &amp;amp;varname = ' ' then &amp;amp;varname = &amp;amp;&amp;amp;&amp;amp;varname;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the programming would be that easy, it must be worthwhile to investigate ... how do you take your original set of macro variables and create the second set from them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In most cases, getting commas out of macro variables can only help. &amp;nbsp;The simplest way to do that is in a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;call symputx('a', translate("&amp;amp;a", ' ', ','));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other transformations can be handled by macro language:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;b));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%let &amp;amp;scan(&amp;amp;b, &amp;amp;i) = %scan(&amp;amp;b2, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Done, and ready to use in the relatively simple solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's the quick tour (omitting %local statements).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 13:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/recode-a-variable-within-a-data-step-using-macro-variable/m-p/378121#M90832</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-21T13:17:20Z</dc:date>
    </item>
  </channel>
</rss>

