<?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: Best way to swap certain values of variables that were input in wrong columns in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948587#M42613</link>
    <description>&lt;P&gt;In general to swap two values you need a third variable to hold one of them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_temp_=LEFT;
left=RIGHT;
right=_temp_;
drop _temp_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is your test that indicates the HT and WT should be swapped?&lt;/P&gt;</description>
    <pubDate>Tue, 22 Oct 2024 15:29:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-22T15:29:32Z</dc:date>
    <item>
      <title>Best way to swap certain values of variables that were input in wrong columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948585#M42612</link>
      <description>&lt;P&gt;I have a dataset where it appears some ht and wt values were in put in the wrong column (e.g., ht in wt and vice versa).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My gorilla code (see: use brute force) below worked, but I'm wondering if I could do this more succinctly, i.e., swap ht and wt values for certain observations meeting conditions specified by my IF statements.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I also recognize I could combine the two datasteps below into one, and subsequently did, but wanted to be able to check my code was doing what I wanted in between. This is the same reason I didn't drop my htnew2/wtnew2 variables.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data htwt;
	set wtnew;
	htnew2=0; /*added because otherwise this variable was uninitialized*/
	wtnew2=0; /*added because otherwise this variable was uninitialized*/
	IF 0&amp;lt;htnew&amp;lt;150 and wtnew&amp;gt;110 then htnew2=wtnew; 
	IF 0&amp;lt;htnew&amp;lt;150 and wtnew&amp;gt;110 then wtnew2=htnew;
	run;
&lt;BR /&gt;/*I then checked these variables with proc freq; also ran proc print of those observations*/&lt;BR /&gt;
data htwtcorrect;
	set htwt;
	IF wtnew2&amp;gt;0 and htnew2&amp;gt;0 then htnew=htnew2;
	IF wtnew2&amp;gt;0 and htnew2&amp;gt;0 then wtnew=wtnew2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 15:07:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948585#M42612</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-10-22T15:07:41Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to swap certain values of variables that were input in wrong columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948587#M42613</link>
      <description>&lt;P&gt;In general to swap two values you need a third variable to hold one of them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_temp_=LEFT;
left=RIGHT;
right=_temp_;
drop _temp_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is your test that indicates the HT and WT should be swapped?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 15:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948587#M42613</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-22T15:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to swap certain values of variables that were input in wrong columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948593#M42615</link>
      <description>I believe that is essentially what I did with my htnew2 and wtnew2 variables. Thank you for confirming.&lt;BR /&gt;&lt;BR /&gt;As for my test, I first looked at the distribution of each via proc univariate with robustscale and plot options. Then, I looked at specific observations based on the outliers. &lt;BR /&gt;&lt;BR /&gt;That was where I noticed that observations with nonsensical height and weight were within expected distribution if the variables were reversed, leading me to surmise these were input in reverse.</description>
      <pubDate>Tue, 22 Oct 2024 15:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948593#M42615</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-10-22T15:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to swap certain values of variables that were input in wrong columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948595#M42617</link>
      <description>&lt;P&gt;If you have variables that identify the observations that need correction it is relatively easy. The complexity may come from how many variables are involved and how many observations. If you have a single variable such as an ID variable then a data step with an IN condition:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   set have;
   if idvariable in (123   456   2222) then do;
  &amp;lt;code to execute&amp;gt;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;If the list of Id values is long, or if there are multiple variables needed then a helper data set and a merge.&lt;/P&gt;
&lt;PRE&gt;Proc sort data=have;
  by &amp;lt;list of id variables&amp;gt;;
run;

Proc sort data=helper;
  by &amp;lt;list of id variables&amp;gt;;
run;


 data want;
     merge
         have 
        helper (in=inhelper)
   ;
  by &amp;lt;list of id variables&amp;gt;;
  if inhelper then do;
  &amp;lt;code to do&amp;gt;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;The merge is going to want the two data sets to be sorted by the same values to match up observations. The IN=option creates a numeric 1/0, for true/false, variable that indicates when the current observation has a contribution from the helper data data set. So you can execute code conditionally. For this sort of problem the Helper data set should only have the variables needed to identify the problem observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I do understand the problem. I have had the same thing happen with some child data. In my case it was all observations at a couple of schools so I could use the the first approach with an IN list for the couple of schools involved.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 15:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948595#M42617</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-22T15:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to swap certain values of variables that were input in wrong columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948596#M42618</link>
      <description>&lt;P&gt;This is helpful, thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 15:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Best-way-to-swap-certain-values-of-variables-that-were-input-in/m-p/948596#M42618</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-10-22T15:48:34Z</dc:date>
    </item>
  </channel>
</rss>

