<?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 How to assign a column value based on another column ?? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217728#M40106</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a question about data step.&amp;nbsp; if I have a table with two columns, Type and value.&amp;nbsp; What I am trying to do is for all the same type group records, their values will be assigned the smallest value in this group. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tao;&lt;/P&gt;&lt;P&gt;input type $&amp;nbsp; Mvalue ;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 12&lt;/P&gt;&lt;P&gt;B 15&lt;/P&gt;&lt;P&gt;C 18&lt;/P&gt;&lt;P&gt;C 19&lt;/P&gt;&lt;P&gt;D 23&lt;/P&gt;&lt;P&gt;E 11&lt;/P&gt;&lt;P&gt;E 90&lt;/P&gt;&lt;P&gt;E 100&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the Result I want to see is &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;A 12&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;B 15&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;C 18&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;C 18&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;D 23&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;I can easily find those records need to be changed but not sure how to assign the smallest value in the group. However, I tried several ways to do this and it does not work because lag function always grab the one in the lag queue. For example, code below will not work.&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P&gt;data YYY;&lt;/P&gt;&lt;P&gt;set tao;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if type=lag(type) then mvalue=lag(mvalue);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can also use last.type to find the records, but not sure how to assign the values;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope someone can give me some hints.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tao&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 08 Apr 2015 18:07:48 GMT</pubDate>
    <dc:creator>yangtaotai</dc:creator>
    <dc:date>2015-04-08T18:07:48Z</dc:date>
    <item>
      <title>How to assign a column value based on another column ??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217728#M40106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a question about data step.&amp;nbsp; if I have a table with two columns, Type and value.&amp;nbsp; What I am trying to do is for all the same type group records, their values will be assigned the smallest value in this group. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tao;&lt;/P&gt;&lt;P&gt;input type $&amp;nbsp; Mvalue ;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 12&lt;/P&gt;&lt;P&gt;B 15&lt;/P&gt;&lt;P&gt;C 18&lt;/P&gt;&lt;P&gt;C 19&lt;/P&gt;&lt;P&gt;D 23&lt;/P&gt;&lt;P&gt;E 11&lt;/P&gt;&lt;P&gt;E 90&lt;/P&gt;&lt;P&gt;E 100&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the Result I want to see is &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;A 12&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;B 15&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;C 18&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;C 18&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;D 23&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;E 11&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;I can easily find those records need to be changed but not sure how to assign the smallest value in the group. However, I tried several ways to do this and it does not work because lag function always grab the one in the lag queue. For example, code below will not work.&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P&gt;data YYY;&lt;/P&gt;&lt;P&gt;set tao;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if type=lag(type) then mvalue=lag(mvalue);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can also use last.type to find the records, but not sure how to assign the values;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope someone can give me some hints.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tao&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 18:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217728#M40106</guid>
      <dc:creator>yangtaotai</dc:creator>
      <dc:date>2015-04-08T18:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a column value based on another column ??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217729#M40107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use the min() with sql:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table want as&lt;/P&gt;&lt;P&gt;select *,min(Mvalue) as min_Mvalue&lt;/P&gt;&lt;P&gt;from have&lt;/P&gt;&lt;P&gt;group by type&lt;/P&gt;&lt;P&gt;order by type;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 18:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217729#M40107</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-04-08T18:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a column value based on another column ??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217730#M40108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Mark,&lt;/P&gt;&lt;P&gt;Thank you for your reply! Your solution definitely works!&amp;nbsp;&amp;nbsp; However, I wonder if there is a way in data step can do the same thing?&amp;nbsp; I believe there should be but I just cannot figure it out. If it is not easy to do in data step, then in the future for this kind of issue, I probably will keep using proc sql way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tao&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 18:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217730#M40108</guid>
      <dc:creator>yangtaotai</dc:creator>
      <dc:date>2015-04-08T18:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a column value based on another column ??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217731#M40109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will get you the same thing in a datastep:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tao_want;&lt;/P&gt;&lt;P&gt;set tao;&lt;/P&gt;&lt;P&gt;retain m_value;&lt;/P&gt;&lt;P&gt;by type;&lt;/P&gt;&lt;P&gt;if first.type then m_value = value;&lt;/P&gt;&lt;P&gt;if m_value &amp;gt; value then m_value = value;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 18:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-column-value-based-on-another-column/m-p/217731#M40109</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-04-08T18:53:08Z</dc:date>
    </item>
  </channel>
</rss>

