<?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: Invert values from a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687592#M208752</link>
    <description>&lt;P&gt;&amp;nbsp;Hard to argue with the simplicity of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s solution, but another approach using a Macro to generate SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS	MPRINT;

%LET	Max_Value	=	5;
%LET	Min_Value	=	1;
%LET	Nbr_Vars	=	3;

**------------------------------------------------------------------------------**;

%MACRO	Generate_Inversion_Code;
	%DO	j	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;
		%LET	Value_Old&amp;amp;j	=	%EVAL(&amp;amp;Min_Value + &amp;amp;j -1);
		%LET	Value_New&amp;amp;j	=	%EVAL(&amp;amp;Max_Value - &amp;amp;j +1);
	%END;

	%**	Debugging Code	**;
/*	%DO	k	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;*/
/*		%PUT	&amp;amp;Nte1  &amp;amp;k = &amp;amp;&amp;amp;Value_Old&amp;amp;k;*/
/*		%PUT	&amp;amp;Nte2  &amp;amp;k = &amp;amp;&amp;amp;Value_New&amp;amp;k;*/
/*	%END;*/

	%DO	i	=	1	%TO	&amp;amp;Nbr_Vars;
		%DO	j	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;
			%IF	&amp;amp;j	=	&amp;amp;Max_Value	%THEN
				%DO;
					IF	Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_Old&amp;amp;j	THEN
						Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_New&amp;amp;j;
				%END;
			%ELSE
				%DO;
					IF	Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_Old&amp;amp;j	THEN
						Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_New&amp;amp;j;
					ELSE
				%END;
		%END;
	%END;
%MEND	Generate_Inversion_Code;

**------------------------------------------------------------------------------**;

DATA	Have;
	input var1 var2 var3;

	%Generate_Inversion_Code;

cards;

4 2 3
3 2 3
3 2 4
4 2 4
1 3 4
2 3 2
4 4 2
1 4 4
4 4 3
3 4 3
4 4 2
2 4 2
1 4 2
2 4 5
5 1 1
2 4 4
4 3 4
5 2 3
4 2 5
5 4 3
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1601398854364.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49943iB20895CE3E604E77/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1601398854364.png" alt="jimbarbour_0-1601398854364.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 17:01:03 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2020-09-29T17:01:03Z</dc:date>
    <item>
      <title>Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687577#M208746</link>
      <description>&lt;P&gt;Hi friends:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input var1 var2 var3;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;4 2 3&lt;BR /&gt;3 2 3&lt;BR /&gt;3 2 4&lt;BR /&gt;4 2 4&lt;BR /&gt;1 3 4&lt;BR /&gt;2 3 2&lt;BR /&gt;4 4 2&lt;BR /&gt;1 4 4&lt;BR /&gt;4 4 3&lt;BR /&gt;3 4 3&lt;BR /&gt;4 4 2&lt;BR /&gt;2 4 2&lt;BR /&gt;1 4 2&lt;BR /&gt;2 4 5&lt;BR /&gt;5 1 1&lt;BR /&gt;2 4 4&lt;BR /&gt;4 3 4&lt;BR /&gt;5 2 3&lt;BR /&gt;4 2 5&lt;BR /&gt;5 4 3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i need to replace values just in var1 and var3 with this rule:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if var1 = 5 then var1 = 1&lt;/P&gt;
&lt;P&gt;if var1 = 4 then var1 = 2&lt;/P&gt;
&lt;P&gt;if var1 = 2 then var1 = 4&lt;/P&gt;
&lt;P&gt;if var1 = 1 then var1 = 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i shod do this to all variables, but lets supose that i have 35 variables, it can be much time&amp;nbsp; to do this manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help pls&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687577#M208746</guid>
      <dc:creator>jonatan_velarde</dc:creator>
      <dc:date>2020-09-29T16:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687590#M208751</link>
      <description>&lt;P&gt;Simple math&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var1=6-var1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do this for 35 variables, use an array&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    array v var1-var35;
    do i=1 to dim(v);
        v(i)=6-v(i);
    end;
    drop i;
run;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687590#M208751</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-29T16:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687592#M208752</link>
      <description>&lt;P&gt;&amp;nbsp;Hard to argue with the simplicity of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s solution, but another approach using a Macro to generate SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS	MPRINT;

%LET	Max_Value	=	5;
%LET	Min_Value	=	1;
%LET	Nbr_Vars	=	3;

**------------------------------------------------------------------------------**;

%MACRO	Generate_Inversion_Code;
	%DO	j	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;
		%LET	Value_Old&amp;amp;j	=	%EVAL(&amp;amp;Min_Value + &amp;amp;j -1);
		%LET	Value_New&amp;amp;j	=	%EVAL(&amp;amp;Max_Value - &amp;amp;j +1);
	%END;

	%**	Debugging Code	**;
/*	%DO	k	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;*/
/*		%PUT	&amp;amp;Nte1  &amp;amp;k = &amp;amp;&amp;amp;Value_Old&amp;amp;k;*/
/*		%PUT	&amp;amp;Nte2  &amp;amp;k = &amp;amp;&amp;amp;Value_New&amp;amp;k;*/
/*	%END;*/

	%DO	i	=	1	%TO	&amp;amp;Nbr_Vars;
		%DO	j	=	&amp;amp;Min_Value	%TO	&amp;amp;Max_Value;
			%IF	&amp;amp;j	=	&amp;amp;Max_Value	%THEN
				%DO;
					IF	Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_Old&amp;amp;j	THEN
						Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_New&amp;amp;j;
				%END;
			%ELSE
				%DO;
					IF	Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_Old&amp;amp;j	THEN
						Var&amp;amp;i	=	&amp;amp;&amp;amp;Value_New&amp;amp;j;
					ELSE
				%END;
		%END;
	%END;
%MEND	Generate_Inversion_Code;

**------------------------------------------------------------------------------**;

DATA	Have;
	input var1 var2 var3;

	%Generate_Inversion_Code;

cards;

4 2 3
3 2 3
3 2 4
4 2 4
1 3 4
2 3 2
4 4 2
1 4 4
4 4 3
3 4 3
4 4 2
2 4 2
1 4 2
2 4 5
5 1 1
2 4 4
4 3 4
5 2 3
4 2 5
5 4 3
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1601398854364.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49943iB20895CE3E604E77/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1601398854364.png" alt="jimbarbour_0-1601398854364.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687592#M208752</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-29T17:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687593#M208753</link>
      <description>awesome, this answer is very clever.&lt;BR /&gt;&lt;BR /&gt;Just to be clear, how could be the current code if i just need to replave var1 and var3&lt;BR /&gt;&lt;BR /&gt;thanks in advance</description>
      <pubDate>Tue, 29 Sep 2020 17:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687593#M208753</guid>
      <dc:creator>jonatan_velarde</dc:creator>
      <dc:date>2020-09-29T17:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687595#M208754</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50712"&gt;@jonatan_velarde&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;awesome, this answer is very clever.&lt;BR /&gt;&lt;BR /&gt;Just to be clear, how could be the current code if i just need to replave var1 and var3&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array v var1 var3;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687595#M208754</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-29T17:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687596#M208755</link>
      <description>Already did this and apears whole variables in the original data</description>
      <pubDate>Tue, 29 Sep 2020 17:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687596#M208755</guid>
      <dc:creator>jonatan_velarde</dc:creator>
      <dc:date>2020-09-29T17:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: Invert values from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687597#M208756</link>
      <description>&lt;P&gt;If this is something that you may have to do repeatedly then perhaps reading the data with a custom informat would be a better solution than fixing afterwards.&lt;/P&gt;
&lt;P&gt;This would assume that you would typically read the data from a text file with a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format;
invalue invert
1 = 5
2 = 4
3 = 3
4 = 2
5 = 1
;
run;

data example;
  informat x1 x2 x3 invert.;
  input x1 x2 x3;
datalines;
1 2 3
4 5 1
2 3 4
5 1 2
;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Invert-values-from-a-dataset/m-p/687597#M208756</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-29T17:09:51Z</dc:date>
    </item>
  </channel>
</rss>

