<?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: SQL on name of variable and value of variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434474#M107821</link>
    <description>&lt;P&gt;And what is the condition?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Feb 2018 07:27:03 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-02-06T07:27:03Z</dc:date>
    <item>
      <title>SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434469#M107818</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I have 2 files:&lt;/P&gt;
&lt;P&gt;1 file contains variable name and variable value&lt;/P&gt;
&lt;P&gt;1 file contain value for each variable and and Result variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to merge the 2 file so that for each row in file name_value, I can get all record that meet condition in file 1.&lt;/P&gt;
&lt;P&gt;For example, for the first row '&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1 a 1 b 2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the result should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;1 a 1 b 1&amp;nbsp; &amp;nbsp;&amp;nbsp;1 2 3 1000&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;1 a 1 b 1&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;1 2&amp;nbsp;1&amp;nbsp; 999&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2nd row has no record met condition&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3rd row has 1 record&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3 b 3 c 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1 3 1 888&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any help is very much appreciated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;HHCFX&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*This code below will get data for each condition*******************/&lt;/P&gt;
&lt;P&gt;data condition; &lt;BR /&gt;input condition_ID cond1 :$1. value1 cond2 :$1. value2;&lt;BR /&gt;datalines;&lt;BR /&gt;1 a 4 b 5&lt;BR /&gt;2 c 1 d 2&lt;BR /&gt;3 c 9 d 9&lt;BR /&gt;;run;&lt;BR /&gt;data FULLDATA; &lt;BR /&gt;input date a b c d ;&lt;BR /&gt;datalines;&lt;BR /&gt;11 4 5 1 1&lt;BR /&gt;12 4 5 2 5&lt;BR /&gt;13 4 1 2 6&lt;BR /&gt;14 8 3 1 2&lt;BR /&gt;15 4 5 1 2&lt;BR /&gt;;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;filename tmp temp;&lt;BR /&gt;data _null_;&lt;BR /&gt; set condition end=last_cond;&lt;BR /&gt; file tmp;&lt;BR /&gt; put 'if ' cond1 '=' value1 'and ' cond2 '=' value2&lt;BR /&gt; ' then do; condition_id=' condition_id '; output;end;' ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;options source2;&lt;BR /&gt;data want;&lt;BR /&gt; set fulldata;&lt;BR /&gt; %include tmp;&lt;BR /&gt; if condition_id &amp;gt;=1;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 16:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434469#M107818</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2018-02-06T16:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434474#M107821</link>
      <description>&lt;P&gt;And what is the condition?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 07:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434474#M107821</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-02-06T07:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434477#M107822</link>
      <description>I can't see a beautiful way to solve this - haven't had a coffee, so that could be the cause of lacking good ideas. What have you tried so far? Are there always just two key-value-pairs in "name_value"? Is seems that transposing that dataset to have exactly one key-value-pair in each observation a good start.</description>
      <pubDate>Tue, 06 Feb 2018 07:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434477#M107822</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2018-02-06T07:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434509#M107830</link>
      <description>&lt;P&gt;Here is a possible solution. I am not sure how fast it runs with real-life data, but it seems to work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set name_value;
  if 0 then set original; /* "declare" variables, for use with CALL SET later */
  array names(*) var_name1-var_name2;
  array values(*) var_value1-var_value2;
  length cond $100;
  do _N_=1 to dim(names);
    call catx(' and ',cond,cats(names(_N_),'=',values(_N_)));
    end;
  dsid=open(cats('original(where=(',cond,'))'));
  call set(dsid);
  do while(fetch(dsid)=0);
    output;
    end;
  dsid=close(dsid);
  drop dsid;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I did not drop the condition (COND variable), as it is nice to have for checking, at least initially.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 11:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434509#M107830</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-06T11:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434518#M107831</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this should work :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data name_value;
    set name_value;
    array abc (3) 4. a b c;

    do i=1 to dim(abc);
        if var_name1=vname(abc(i)) then abc(i)=var_value1;
        if var_name2=vname(abc(i)) then abc(i)=var_value2;
    end;
run;

proc sql noprint;
    CREATE TABLE want AS
    SELECT a.name_ID, var_name1, var_value1, var_name2, var_value2, b.*
    FROM name_value a
    LEFT JOIN original b
    ON (a.a=b.a OR a.a=.) AND (a.b=b.b OR a.b=.) AND (a.c=b.c OR a.c=.);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Feb 2018 12:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434518#M107831</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-02-06T12:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: SQL on name of variable and value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434520#M107832</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data name_value;
input name_ID var_name1 $ var_value1 var_name2 $ var_value2;
datalines;
1 a 1 b 2
2 b 1 c 0
3 b 3 c 1
;run;

data original;
input a b c result;
datalines;
1 2 3 1000
1 2 1 9999
1 3 1 8888
0 0 9 1000
;run; 

data k;
 set original;
 length var_name1 var_name2 $ 32;
 array x{*} a--c;
 do i=1 to dim(x);
  do j=i+1 to dim(x);
    var_name1=vname(x{i});var_value1=x{i};
    var_name2=vname(x{j});var_value2=x{j};
    output;
    var_name1=vname(x{j});var_value1=x{j};
    var_name2=vname(x{i});var_value2=x{i};
    output;
  end;
 end;
drop i j;
run;
data want;
 if _n_=1 then do;
   if 0 then set k;
   declare hash k(dataset:'k',multidata:'y');
   k.definekey('var_name1','var_value1','var_name2','var_value2');
   k.definedata(all:'y');
   k.definedone();
 end;
 set name_value;
rc=k.find();
if rc ne 0 then output;
do while (rc=0);
  output;
  rc=k.find_next();  
end;
call missing(of _all_);
 drop rc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Feb 2018 13:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-on-name-of-variable-and-value-of-variable/m-p/434520#M107832</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-02-06T13:07:20Z</dc:date>
    </item>
  </channel>
</rss>

