<?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: creating a new variable based on a variable in another data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598869#M172762</link>
    <description>&lt;P&gt;Yes it does. Thank you!&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2019 23:47:33 GMT</pubDate>
    <dc:creator>ajb</dc:creator>
    <dc:date>2019-10-23T23:47:33Z</dc:date>
    <item>
      <title>creating a new variable based on a variable in another data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598599#M172639</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set called &lt;STRONG&gt;&lt;EM&gt;metabolic &lt;/EM&gt;&lt;/STRONG&gt;and I would like to calculate the median value of a specific variable &lt;EM&gt;&lt;STRONG&gt;sysbp. &lt;/STRONG&gt;&lt;/EM&gt;I would then like to create a new variable in &lt;STRONG&gt;&lt;EM&gt;metabolic&lt;/EM&gt;&lt;/STRONG&gt; called &lt;STRONG&gt;&lt;EM&gt;median&lt;/EM&gt;&lt;/STRONG&gt;, and give all observations the same median value calculated in med data set.&amp;nbsp; From this I want to calculate the absolute difference between &lt;STRONG&gt;&lt;EM&gt;sysbp&lt;/EM&gt;&lt;/STRONG&gt; and &lt;EM&gt;&lt;STRONG&gt;median&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; I need to automate this as I will eventually need to create a marco.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code that I have been playing with in &lt;STRONG&gt;SAS 9.4&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc univariate data=metabolic noprint;&lt;BR /&gt;var sysbp;&lt;BR /&gt;output out=med &lt;BR /&gt;mean=mean &lt;BR /&gt;median=median &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data new;&lt;BR /&gt;merge metabolic&lt;BR /&gt;med(keep=median);&lt;BR /&gt;diff = abs(sysbp - median);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;Med data set&lt;/FONT&gt; &amp;nbsp; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" color="#0000FF"&gt;&amp;nbsp; mean&amp;nbsp; median&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" color="#0000FF"&gt;1 130&amp;nbsp;&amp;nbsp; 134&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;&lt;STRONG&gt;Metabolic data set&lt;/STRONG&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID&amp;nbsp; SYSBP&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;1&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp; 135&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;2&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp; 102&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;3&amp;nbsp;&amp;nbsp; 13&amp;nbsp;&amp;nbsp; 145 &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080"&gt;n&amp;nbsp;&amp;nbsp; nn&amp;nbsp;&amp;nbsp; 101&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;Wanted: New data set&lt;/STRONG&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#993366"&gt;ID&amp;nbsp; SYSBP&lt;/FONT&gt; Median&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;1&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp; 135&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp; 134&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;2&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp; 102&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp; 134&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;3&amp;nbsp;&amp;nbsp; 13&amp;nbsp;&amp;nbsp; 145&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp; 134&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;n&amp;nbsp;&amp;nbsp; nn&amp;nbsp;&amp;nbsp; 101&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp; 134 &lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;Incorrect data set that I keep getting&amp;nbsp;&lt;/STRONG&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#993366"&gt;ID&amp;nbsp; SYSBP&lt;/FONT&gt; Median&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;1&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp; 135&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp; 134&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;2&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp; 102&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp; .&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;3&amp;nbsp;&amp;nbsp; 13&amp;nbsp;&amp;nbsp; 145&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#800080"&gt;n&amp;nbsp;&amp;nbsp; nn&amp;nbsp;&amp;nbsp; 101&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;I have also tried this code and it did not give me the right data set either&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;data new;&lt;BR /&gt;merge metabolic&lt;BR /&gt;med(keep=median);&lt;/P&gt;
&lt;P&gt;if first.habcid then median2=median;&lt;BR /&gt;else median2=first.median; &lt;BR /&gt;diff = abs(sysbp - median2);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am very rusty with SAS so any help in tweeking the code is greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 04:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598599#M172639</guid>
      <dc:creator>ajb</dc:creator>
      <dc:date>2019-10-23T04:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: creating a new variable based on a variable in another data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598606#M172642</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17666"&gt;@ajb&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need one value (MEDIAN) from MED data set, it can be done as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
   if _n_ = 1 then set med(keep=median);
   set metabolic;
   diff = abs(sysbp - median);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Oct 2019 04:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598606#M172642</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-10-23T04:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: creating a new variable based on a variable in another data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598850#M172750</link>
      <description>&lt;P&gt;The way you do it, SAS tries to find the second observation of the median table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;'s code, SAS only reads the first observation of the median table, and because it doesn't try to access it again, the PDV is never reinitialised for the median variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this makes sense.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 00:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598850#M172750</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-24T00:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: creating a new variable based on a variable in another data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598868#M172761</link>
      <description>&lt;P&gt;Thank you! I have never seen two set statements used in this way. I had to read up on it. I have learnt something new.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 23:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598868#M172761</guid>
      <dc:creator>ajb</dc:creator>
      <dc:date>2019-10-23T23:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: creating a new variable based on a variable in another data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598869#M172762</link>
      <description>&lt;P&gt;Yes it does. Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 23:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-new-variable-based-on-a-variable-in-another-data-set/m-p/598869#M172762</guid>
      <dc:creator>ajb</dc:creator>
      <dc:date>2019-10-23T23:47:33Z</dc:date>
    </item>
  </channel>
</rss>

