<?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: How to assign a default value to a variable during the join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360152#M84734</link>
    <description>&lt;P&gt;That sounds like a CASE statement. The only issue I can see is using the wildcard (*) if the names overlap, if this is a new variable it will be fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample - untested.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Case when state='CA' then R2&lt;/P&gt;
&lt;P&gt;when state='PA' then r3&lt;/P&gt;
&lt;P&gt;end as new_variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2017 23:23:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-05-19T23:23:01Z</dc:date>
    <item>
      <title>How to assign a default value to a variable during the join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360150#M84733</link>
      <description>&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;SPAN style="color: rgb(31, 73, 125);"&gt;&lt;FONT face="Calibri" size="3"&gt;Hi,&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;I have a question related to SAS join.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;I have a dataset one which has the variable country, year,state&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Country year state match&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT face="Calibri"&gt;&lt;FONT color="#000000" size="3"&gt;USA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2017` CA&amp;nbsp;&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT color="#000000" size="3"&gt; USA &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2017&amp;nbsp;&amp;nbsp; PA&amp;nbsp;&amp;nbsp;&amp;nbsp; y&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;USA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2016&amp;nbsp;&amp;nbsp;&amp;nbsp; TX&amp;nbsp;&amp;nbsp; y &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;I have dataset two which is a lookup table with few rows and has the columns country,year state , region.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Country &amp;nbsp;year state region&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;USA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2017&amp;nbsp; CA&amp;nbsp;&amp;nbsp; R1&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt; USA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2017&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; R2&lt;BR /&gt; USA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2017 PA&amp;nbsp;&amp;nbsp;&amp;nbsp; R3&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;I am looking to attach region to the dataset one&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Proc sql;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Select a.* ,b.region from one a&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Left join &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Two b&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;On a. year=b.year and&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;a.country =b. country&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;and a.state=b.state;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;is there any way I can modify the above code so that for all the states except , CA and PA(states which are existing&amp;nbsp; in the second table) the value of R2 gets assigned during the join..&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Editing to add more details.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;for a record in dataset one if the country is USA and year is 2017 and state is CA the region needs to be R1,&lt;FONT face="Calibri"&gt;if the country is USA and year is 2017 and state is PA the region needs to be R3 and for all the other states whose year is 2017 and country is USA the region needs to be r2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 0pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Thanks in advance,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Sheeba&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 23:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360150#M84733</guid>
      <dc:creator>Sheeba</dc:creator>
      <dc:date>2017-05-19T23:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a default value to a variable during the join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360152#M84734</link>
      <description>&lt;P&gt;That sounds like a CASE statement. The only issue I can see is using the wildcard (*) if the names overlap, if this is a new variable it will be fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample - untested.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Case when state='CA' then R2&lt;/P&gt;
&lt;P&gt;when state='PA' then r3&lt;/P&gt;
&lt;P&gt;end as new_variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 23:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360152#M84734</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-19T23:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a default value to a variable during the join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360153#M84735</link>
      <description>&lt;P&gt;Assuming that there might also be other countries in your data and R2 only applies to USA the following should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input (Country year state match) (:$);
  datalines;
USA 2017 CA Y
USA 2017 PA y
USA 2016 TX y
run;

data two;
  infile datalines truncover dsd dlm=' ';
  input (Country year state region) (:$);
  datalines;
USA 2017 CA R1
USA 2017  R2
USA 2017 PA R3
;
run;

Proc sql;
  Select 
      a.* ,
      case
        when(a.country='USA') then coalesce(b.region,'R2') 
        else b.region
        end as region 
    from one a
    Left join
        Two b
        on a.country=b.country and
        a.year=b.year and
        and a.state=b.state;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you've got only USA then remove the CASE as the COALESCE() bit will do.&lt;/P&gt;
&lt;P&gt;coalesce(b.region,'R2') as region&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 00:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360153#M84735</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-05-20T00:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a default value to a variable during the join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360157#M84736</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot for the reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will try out the case statements. I need to have it as a same variable as i am using it in the same program. I will try to modify this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Sheeba&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 00:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360157#M84736</guid>
      <dc:creator>Sheeba</dc:creator>
      <dc:date>2017-05-20T00:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a default value to a variable during the join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360158#M84737</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot for the detailed reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is really helpful. Country is likely to remain the same , the year might change. I will modify the case statement accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Sheeba&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 00:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-default-value-to-a-variable-during-the-join/m-p/360158#M84737</guid>
      <dc:creator>Sheeba</dc:creator>
      <dc:date>2017-05-20T00:24:09Z</dc:date>
    </item>
  </channel>
</rss>

