<?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: Pick out macro variables using iterative value in data step in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518864#M73345</link>
    <description>&lt;P&gt;Please don't ever assume. Test thoroughly and if it doesn't work, come back to us. Simple as that!&lt;/P&gt;</description>
    <pubDate>Wed, 05 Dec 2018 16:47:45 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-12-05T16:47:45Z</dc:date>
    <item>
      <title>Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518851#M73340</link>
      <description>&lt;P&gt;I have data of the following basic forma (two columns). Well call this D1 for the data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Index&amp;nbsp; &amp;nbsp; &amp;nbsp; Target&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.5&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to replace the missing values in "Target" by the first non-missing value, and fill every missing value after the first non missing target with the previous non-missing. Here's the "end" result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Index&amp;nbsp; &amp;nbsp; &amp;nbsp; Target&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4*&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4*&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4*&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4*&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.5&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.5*&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* is for the imputed values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, I started by created the following data step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*This step creates macro variables for every index of the first non-missing value*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set D1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;retain j ind;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by index;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if _n_&amp;nbsp; = 1 then j = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if first.index then ind = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if Target ^= '.' and ind = 0 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;call symput('FirstTarget'||strip(left(j)), 'Target');&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;ind = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Now I want to apply those macro variables to those values...and where my code doesn't work*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data D1new;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set D1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; by index;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; retain ind j LastTarget;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*If it's not the first missing value then use the last value, which has already been imputed or already existed*/&lt;/P&gt;&lt;P&gt;if Target = '.' and ind = 1 then Target = LastTarget;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* If it's the first missing value then apply the stored first non-missing value */&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if Target = '.' and ind = 0 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; ind = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; Target = &amp;amp;&amp;amp;FirstTarget&amp;amp;j;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;/*For next iteration, create lagging variable that will remember the previous target value to use for imputation if the next value is missing. This takes care of the 'forward' missing values.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; LastTarget = Target;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;drop j ind;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem lies with j. I've tried using call symput('j', j); or %let j = j; but it doesn't recognize j as a -number- but instead sees j as literally 'j' and tries to interpret &amp;amp;&amp;amp;FirstTarget&amp;amp;j as &amp;amp;FirstTargetj which doesn't exist (and won't work).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a larger number of indexes than two, and I figure understanding this will help me develop my SAS skills to using more than just the bare skills I have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At worst case, I could split the data out by each Index and do it with a macro and $do i in 1 %to &amp;amp;n; but that seems unnecessary/overly long/non-elegant.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518851#M73340</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-12-05T16:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518853#M73341</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Index  $    Target;
cards;
A             .
A             .
A             .
A            4
A            3
B            .
B            4
B           3.5
B           .
;



data want;
if _n_=1 then do;
dcl hash h(dataset:'have(where=(Target ne .))');
h.definekey('index');
h.definedata('Target');
h.definedone();
end;
do until(last.index);
update have(obs=0) have;
by index;
if missing(Target) then rc=h.find();
output;
end;
drop rc ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518853#M73341</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-05T16:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518856#M73342</link>
      <description>&lt;P&gt;Thanks for the solution. I don't understand hash though. I suppose that's up to me to go figure out how your code works.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518856#M73342</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-12-05T16:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518857#M73343</link>
      <description>&lt;P&gt;Ok a simple one--&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data int1;
set have;
by index;
retain _t;
if first.index then _t = target;
if target = .
then target = _t;
else _t = target;
order = _n_;
drop _t;
run;

proc sort data=int1;
by index descending order;
run;

data int2;
set int1;
by index;
retain _t;
if first.index then _t = target;
if target = .
then target = _t;
else _t = target;
drop _t;
run;

proc sort
  data=int2
  out=want (drop=order)
;
by order;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518857#M73343</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-05T16:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518863#M73344</link>
      <description>&lt;P&gt;Thanks so much for your help. i haven't rant this through yet but I'm assuming it works.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your quick responses were very helpful.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518863#M73344</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-12-05T16:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Pick out macro variables using iterative value in data step</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518864#M73345</link>
      <description>&lt;P&gt;Please don't ever assume. Test thoroughly and if it doesn't work, come back to us. Simple as that!&lt;/P&gt;</description>
      <pubDate>Wed, 05 Dec 2018 16:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Pick-out-macro-variables-using-iterative-value-in-data-step/m-p/518864#M73345</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-05T16:47:45Z</dc:date>
    </item>
  </channel>
</rss>

