<?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 find current value of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375525#M276475</link>
    <description>&lt;P&gt;Thanks for reply. CURRENT value is nothing but value of that variable for given ptid at that visit.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2017 22:20:39 GMT</pubDate>
    <dc:creator>Neeta</dc:creator>
    <dc:date>2017-07-12T22:20:39Z</dc:date>
    <item>
      <title>Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375522#M276473</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm working on a data management query report. I've a dataset which stores information about variables where query was placed. Based on that information, I've to find current value of the quaried variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data query;&lt;BR /&gt;input ptid visit source_data $ var $;&lt;BR /&gt;cards;&lt;BR /&gt;1 10 PE HEIGHT&lt;BR /&gt;1 10 AE AEPT&lt;BR /&gt;1 999 DM RACE&lt;BR /&gt;2 20 PE WEIGHT&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data PE;&lt;BR /&gt;input ptid visit HEIGHT WEIGHT;&lt;BR /&gt;cards;&lt;BR /&gt;1 10 5 60&lt;BR /&gt;1 20 6 80&lt;BR /&gt;1 30 4 40&lt;BR /&gt;2 10 5 60&lt;BR /&gt;2 20 6 80&lt;BR /&gt;2 30 4 40&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data AE;&lt;BR /&gt;input ptid visit AEPT $ AESEV;&lt;BR /&gt;cards;&lt;BR /&gt;1 10 COUGH 1&lt;BR /&gt;1 20 DIZZY 2&lt;BR /&gt;1 30 FATIGUE 3&lt;BR /&gt;2 10 FEVER 4&lt;BR /&gt;2 20 NAUSEA 3&lt;BR /&gt;2 30 VOMIT 1&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data DM;&lt;BR /&gt;input ptid visit RACE $;&lt;BR /&gt;cards;&lt;BR /&gt;1 999 ASIAN&lt;BR /&gt;2 999 AMERICAN&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is desired output data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data desired;&lt;BR /&gt;input ptid visit source_data $ var $ current $;&lt;BR /&gt;cards;&lt;BR /&gt;1 10 PE HEIGHT 5&lt;BR /&gt;1 10 AE AEPT COUGH&lt;BR /&gt;1 999 DM RACE ASIAN&lt;BR /&gt;2 20 PE WEIGHT 80&lt;BR /&gt;;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In reality, I've hundreads of records and ~ 50 variables in each dataset. I would like to know efficient way of solving this problem!&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 22:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375522#M276473</guid>
      <dc:creator>Neeta</dc:creator>
      <dc:date>2017-07-12T22:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375523#M276474</link>
      <description>&lt;P&gt;Not sure what you mean by CURRENT value, but it looks like you have simple code generation problem.&lt;/P&gt;
&lt;P&gt;Figure out what code you would need to get your desired result and then figure out how to generate that code from your input metadata table. &amp;nbsp;Since your queries will need to analyze many input tables you might want to do it either one record of your metadata table at a time. &amp;nbsp;But you could also possible group the metadata by the source data table and try to generate the results for all queries against that table at the same time.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 22:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375523#M276474</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-12T22:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375525#M276475</link>
      <description>&lt;P&gt;Thanks for reply. CURRENT value is nothing but value of that variable for given ptid at that visit.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 22:20:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375525#M276475</guid>
      <dc:creator>Neeta</dc:creator>
      <dc:date>2017-07-12T22:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375533#M276476</link>
      <description>&lt;P&gt;I don't see anything here besides the need for a join/merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need help with understanding how to do that?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/match-merging-data-files-in-sas/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/match-merging-data-files-in-sas/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though to be honest, usually most people who can't join can't prep sample data so is this homework or work?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 22:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375533#M276476</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-12T22:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375539#M276477</link>
      <description>&lt;P&gt;So let's see how we could find the records you want from the PE dataset. &amp;nbsp;Something like this might do the trick.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data next ;
  length ptid visit 8 source_data var $32 current $32 ;
  keep ptid -- current ;
  set pe ;
  retain source_data 'PE';
  if ptid=1 and visit=10 then do;
    var = 'HEIGHT';
    current=vvalue(HEIGHT);
    output;
  end;
  if ptid=2 and visit=20 then do;
    var = 'WEIGHT';
    current=vvalue(WEIGHT);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So then it should be easy to generate code like that for each of the source datasets.&lt;/P&gt;
&lt;P&gt;So then just generate one of those for each value of SOURCE_DATA in the metadata file. Along with some PROC APPEND code to aggregate them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=query ;
  by source_data ptid visit var ;
run;

filename code temp;
data _null_;
  set query;
  by source_data ;
  file code ;
  if _n_=1 then put
 'proc delete data=want;'
/'run;'
  ;
  if first.source_data then put 
 'data next ;'
/'  length ptid visit 8 source_data var $32 current $32 ;'
/'  keep ptid -- current ;'
/'  set ' source_data ';'
/'  retain source_data ' source_data :$quote. ';'
  ;
  put
 '  if ' ptid= 'and  ' visit= 'then do;'
/'    var = ' var :$quote. ';'
/'    current=vvalue(' var ');'
/'    output;'
/'  end;'
  ;
  if last.source_data then put
 'run;'
/'proc append base=want data=next force;'
/'run;'
  ;
run;

%include code / source2 ;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                        source_
Obs    ptid    visit     data       var        current

 1       1       10       AE       AEPT      COUGH
 2       1      999       DM       RACE      ASIAN
 3       1       10       PE       HEIGHT               5
 4       2       20       PE       WEIGHT              80
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 23:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375539#M276477</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-12T23:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to find current value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375800#M276478</link>
      <description>Thanks a lot Tom! Your solution worked perfectly!!!</description>
      <pubDate>Thu, 13 Jul 2017 18:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375800#M276478</guid>
      <dc:creator>Neeta</dc:creator>
      <dc:date>2017-07-13T18:47:06Z</dc:date>
    </item>
  </channel>
</rss>

