<?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: if/then macro consecutive variable creation in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709537#M26728</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/247746"&gt;@Scorpx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks Reeza, I will accept your answer as the solution due to the examples you gave me. Evidently, the solution was easier than what I thought it should be.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Large economy sized hint: When you start wanting to do the exact same thing to multiple variables some solution involves one or more arrays. There are may be other approaches but arrays are very powerful.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Jan 2021 00:20:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-01-06T00:20:20Z</dc:date>
    <item>
      <title>if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709526#M26721</link>
      <description>&lt;P&gt;Hello community.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with several (like 90) Likert scale variables, I want to transform them into numerical variables using a macro because each variable is an option of a question. I know I could do it with simple if/then/else but will take me forever and is prone to errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a sample of my data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Scorpx_0-1609887456853.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53234iFF59CD3FE4B04365/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Scorpx_0-1609887456853.png" alt="Scorpx_0-1609887456853.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a sample of what I want that I did one by one:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if v7 in ("7.- Extremely important") then q1_1 = 7;
else if v7 in ("4.- Neutral") then q1_1=4;
else if v7 in ("1.- Not important at all") then q1_1=1;
else if v7 in ("2.-") then q1_1=2;
else if v7 in ("3.-") then q1_1=3;
else if v7 in ("5.-") then q1_1=5;
else q1_1=6;

if v8 in ("7.- Extremely important") then q1_2 = 7;
else if v8 in ("4.- Neutral") then q1_2=4;
else if v8 in ("1.- Not important at all") then q1_2=1;
else if v8 in ("2.-") then q1_2=2;
else if v8 in ("3.-") then q1_2=3;
else if v8 in ("5.-") then q1_2=5;
else q1_2=6;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the macro that I have now:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO Q1;
%DO I = 7 %TO 8;
%DO Z = 1 %TO 2;

%if V&amp;amp;I. ="7.- Extremely important" %then q1_&amp;amp;Z.=7;
%else %if V&amp;amp;I.="4.- Neutral" %then q1_&amp;amp;Z.=4;
%else %if V&amp;amp;I.="1.- Not important at all" %then q1_&amp;amp;Z.=1;
%else %if V&amp;amp;I.="2.-" %then q1_&amp;amp;Z.=2;
%else %if V&amp;amp;I.="3.-" %then q1_&amp;amp;Z.=3;
%else %if V&amp;amp;I.="5.-" %then q1_&amp;amp;Z.=5;
%else q1_&amp;amp;Z.=6;

%END;
%END;
%MEND Q1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I doesn't create the new variables. I would appreciate any suggestion.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 23:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709526#M26721</guid>
      <dc:creator>Scorpx</dc:creator>
      <dc:date>2021-01-05T23:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709529#M26723</link>
      <description>&lt;P&gt;Why did you change IF to %IF? and THEN into %THEN?&lt;/P&gt;
&lt;P&gt;Looks like you just want to change V7 into V&amp;amp;I.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why not just use arrays instead of resorting code generation?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 23:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709529#M26723</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-05T23:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709530#M26724</link>
      <description>&lt;P&gt;IMO you need an array not a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial on using Arrays in SAS&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-arrays/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-arrays/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array old_values(*) v1-v8;
array new_values(*) q1_1- q1_8;

do i=1 to dim(old_values);

if old_values(i) in ("7.- Extremely important") then new_values(i) = 7;
else if old_values(i)  in ("4.- Neutral") then new_values(i) 4;
else if old_values(i)  in ("1.- Not important at all") then new_values(i) =1;
else if old_values(i)  in ("2.-") then new_values(i) =2;
else if old_values(i)  in ("3.-") then new_values(i) =3;
else if old_values(i)  in ("5.-") then new_values(i) =5;
else new_values(i) =6;

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or even better, add in a format.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or since they all have the numbers in the text you could just keep just the numbers and remove all other characters using COMPRESS&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(old_values);

new_values(i) = input(compress(old_values(i) , , 'kd'), 8.);

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/247746"&gt;@Scorpx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with several (like 90) Likert scale variables, I want to transform them into numerical variables using a macro because each variable is an option of a question. I know I could do it with simple if/then/else but will take me forever and is prone to errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a sample of my data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Scorpx_0-1609887456853.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53234iFF59CD3FE4B04365/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Scorpx_0-1609887456853.png" alt="Scorpx_0-1609887456853.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a sample of what I want that I did one by one:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if v7 in ("7.- Extremely important") then q1_1 = 7;
else if v7 in ("4.- Neutral") then q1_1=4;
else if v7 in ("1.- Not important at all") then q1_1=1;
else if v7 in ("2.-") then q1_1=2;
else if v7 in ("3.-") then q1_1=3;
else if v7 in ("5.-") then q1_1=5;
else q1_1=6;

if v8 in ("7.- Extremely important") then q1_2 = 7;
else if v8 in ("4.- Neutral") then q1_2=4;
else if v8 in ("1.- Not important at all") then q1_2=1;
else if v8 in ("2.-") then q1_2=2;
else if v8 in ("3.-") then q1_2=3;
else if v8 in ("5.-") then q1_2=5;
else q1_2=6;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the macro that I have now:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO Q1;
%DO I = 7 %TO 8;
%DO Z = 1 %TO 2;

%if V&amp;amp;I. ="7.- Extremely important" %then q1_&amp;amp;Z.=7;
%else %if V&amp;amp;I.="4.- Neutral" %then q1_&amp;amp;Z.=4;
%else %if V&amp;amp;I.="1.- Not important at all" %then q1_&amp;amp;Z.=1;
%else %if V&amp;amp;I.="2.-" %then q1_&amp;amp;Z.=2;
%else %if V&amp;amp;I.="3.-" %then q1_&amp;amp;Z.=3;
%else %if V&amp;amp;I.="5.-" %then q1_&amp;amp;Z.=5;
%else q1_&amp;amp;Z.=6;

%END;
%END;
%MEND Q1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I doesn't create the new variables. I would appreciate any suggestion.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 23:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709530#M26724</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-05T23:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709531#M26725</link>
      <description>&lt;P&gt;Why all of the IF/THEN logic?&lt;/P&gt;
&lt;P&gt;Why not just generate the value directly from the content?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;q1_1 = input(V7,1.);
q1_2 = input(V8,1.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jan 2021 23:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709531#M26725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-05T23:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709533#M26726</link>
      <description>Thanks, Tom! but I didn't know how to do arrays, I tried with what I know. Evidently, the solution was easier than I thought.&lt;BR /&gt;</description>
      <pubDate>Tue, 05 Jan 2021 23:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709533#M26726</guid>
      <dc:creator>Scorpx</dc:creator>
      <dc:date>2021-01-05T23:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709534#M26727</link>
      <description>Thanks Reeza, I will accept your answer as the solution due to the examples you gave me. Evidently, the solution was easier than what I thought it should be.</description>
      <pubDate>Wed, 06 Jan 2021 00:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709534#M26727</guid>
      <dc:creator>Scorpx</dc:creator>
      <dc:date>2021-01-06T00:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: if/then macro consecutive variable creation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709537#M26728</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/247746"&gt;@Scorpx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks Reeza, I will accept your answer as the solution due to the examples you gave me. Evidently, the solution was easier than what I thought it should be.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Large economy sized hint: When you start wanting to do the exact same thing to multiple variables some solution involves one or more arrays. There are may be other approaches but arrays are very powerful.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 00:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/if-then-macro-consecutive-variable-creation/m-p/709537#M26728</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-06T00:20:20Z</dc:date>
    </item>
  </channel>
</rss>

