<?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: Filling up missing values with previous/next for time series data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719531#M222802</link>
    <description>&lt;P&gt;Basically, you can do this by sorting in descending order, since SAS can only retain the previous value using the retain statement.&lt;BR /&gt;The procedure is to sort in descending order once, fill in the missing values, and then re-sort in ascending order.&lt;/P&gt;
&lt;P&gt;In this case, it is better to keep the observation number as the key, using _n_ in the previous data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* remember number of variables */
data _null_;
  set sashelp.vtable;
  where libname=upcase('WORK') and  memname=upcase('SAMPLE');/* modify this line to your library and dataset name */
  call symputx('nvars',num_numeric+1);/* +1:for key variable */
run;

data want(drop=i);
  set work.sample;/* modify your dataset name */
  key=_n_;
  array _n{&amp;amp;nvars.} _numeric_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  retain _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
run;

data sort; 
  set want;
run;
proc sort data=sort;
  by descending key;
run;

data want(drop=i);
  set work.sort;
  array _n{&amp;amp;nvars.} _numeric_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  retain _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
run;

proc sort data=want out=want(drop=key);
  by key;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Feb 2021 05:19:27 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2021-02-16T05:19:27Z</dc:date>
    <item>
      <title>Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719515#M222787</link>
      <description>&lt;P&gt;I have a time series dataset for 418 variables. I want for all variables to fill up the missing values with the previous available value. If previous value is not available (such that the first value of the time series is missing) then the missing value will be filled up by the next available value. Because I have such a large number of variables I won't be able to fill them up one by one. I want a single set of code that can process for all variables together.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 02:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719515#M222787</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T02:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719517#M222789</link>
      <description>&lt;P&gt;How is this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  length a b c $10 d e f 8;
  infile datalines dsd missover;
  input a b c d e f;
datalines;
aa,bb,  ,1, ,3
aa,  ,cc,1,2,3
dd,x ,  , ,6,  
  ,  ,bc,9, ,7
  ,  ,cc, , ,
;
run;

/* remember number of variables */
data _null_;
  set sashelp.vtable;
  where memname='SAMPLE';
  call symputx('cvars',num_character);
  call symputx('nvars',num_numeric);
run;

data want(drop=i);
  set sample;
  array _n{&amp;amp;nvars.} _numeric_;
  array _c{&amp;amp;cvars.} _character_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  array _rc{&amp;amp;cvars.} $ _temporary_;/* variables for keeping data of previous observation */
  retain _rc: _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
  do i=1 to dim(_c);
    if missing(_c{i}) then _c{i}=_rc{i};
    _rc{i}=_c{i};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 02:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719517#M222789</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-16T02:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719519#M222790</link>
      <description>Sorry but I am quite new to sas and so I was unable to incorporate this code into my datasets. If my understanding is correct, I don't need to run the first few lines as I already have a dataset. So I copied from "/* remember number of variables */" till the end. But I am not sure where to write the name of the dataset, that I have, within the code.</description>
      <pubDate>Tue, 16 Feb 2021 03:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719519#M222790</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T03:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719520#M222791</link>
      <description>&lt;P&gt;OK.&lt;/P&gt;
&lt;P&gt;you need modify 2 statements below.&lt;/P&gt;
&lt;P&gt;if your dataset is temp.original then&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where libname=upcase('temp') and memname=upcase('original ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set temp.original;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That's all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* remember number of variables */
data _null_;
  set sashelp.vtable;
  where libname=upcase('WORK') and  memname=upcase('SAMPLE');/* modify this line to your library and dataset name */
  call symputx('cvars',num_character);
  call symputx('nvars',num_numeric);
run;

data want(drop=i);
  set work.sample;/* modify your dataset name */
  array _n{&amp;amp;nvars.} _numeric_;
  array _c{&amp;amp;cvars.} _character_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  array _rc{&amp;amp;cvars.} $ _temporary_;/* variables for keeping data of previous observation */
  retain _rc: _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
  do i=1 to dim(_c);
    if missing(_c{i}) then _c{i}=_rc{i};
    _rc{i}=_c{i};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 03:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719520#M222791</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-16T03:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719521#M222792</link>
      <description>Thanks for clearing it out. The following error statements came in the log after I ran the code:&lt;BR /&gt;&lt;BR /&gt;ERROR: Invalid dimension specification for array _c. The upper bound of an array dimension is smaller than its corresponding lower bound.&lt;BR /&gt;ERROR: Too few variables defined for the dimension(s) specified for the array _c.&lt;BR /&gt;&lt;BR /&gt;and&lt;BR /&gt;&lt;BR /&gt;ERROR: Invalid dimension specification for array _rc. The upper bound of an array dimension is smaller than its corresponding lower bound.&lt;BR /&gt;</description>
      <pubDate>Tue, 16 Feb 2021 03:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719521#M222792</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T03:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719522#M222793</link>
      <description>&lt;P&gt;Because these is no character variables in your dataset, I think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run below.&lt;/P&gt;
&lt;P&gt;it checks character and numeric variables are exist or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=i);
  set work.sample;/* modify your dataset name */
  
  %if &amp;amp;nvars&amp;gt;0 %then %do;
    array _n{&amp;amp;nvars.} _numeric_;
    array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
    retain _rn:;
    do i=1 to dim(_n);
      if missing(_n{i}) then _n{i}=_rn{i};
      _rn{i}=_n{i};
    end;
  %end;
  %if &amp;amp;cvars&amp;gt;0 %then %do;
    array _c{&amp;amp;cvars.} _character_;
    array _rc{&amp;amp;cvars.} $ _temporary_;/* variables for keeping data of previous observation */
    retain _rc:;
    do i=1 to dim(_c);
      if missing(_c{i}) then _c{i}=_rc{i};
      _rc{i}=_c{i};
    end;
  %end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 04:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719522#M222793</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-16T04:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719527#M222798</link>
      <description>&lt;P&gt;Yes you are right, I don't have any character variables. Sorry, should've mentioned that before. The new code has got the same erorrs for the character variable section. Also it shows some new errors:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: The %IF statement is not valid in open code&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;ERROR: The %END statement is not valid in open code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for patiently replying to all my queries.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 04:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719527#M222798</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T04:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719528#M222799</link>
      <description>&lt;P&gt;OK. this is for only numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=i);
  set work.sample;/* modify your dataset name */
  
  array _n{&amp;amp;nvars.} _numeric_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  retain _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 04:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719528#M222799</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-16T04:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719530#M222801</link>
      <description>Thanks a lot. This works partially. It replaced the missing values with the previous values.&lt;BR /&gt;But when previous values are not available, then it did not take the next available value to replace. Is that possible?</description>
      <pubDate>Tue, 16 Feb 2021 04:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719530#M222801</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T04:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719531#M222802</link>
      <description>&lt;P&gt;Basically, you can do this by sorting in descending order, since SAS can only retain the previous value using the retain statement.&lt;BR /&gt;The procedure is to sort in descending order once, fill in the missing values, and then re-sort in ascending order.&lt;/P&gt;
&lt;P&gt;In this case, it is better to keep the observation number as the key, using _n_ in the previous data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* remember number of variables */
data _null_;
  set sashelp.vtable;
  where libname=upcase('WORK') and  memname=upcase('SAMPLE');/* modify this line to your library and dataset name */
  call symputx('nvars',num_numeric+1);/* +1:for key variable */
run;

data want(drop=i);
  set work.sample;/* modify your dataset name */
  key=_n_;
  array _n{&amp;amp;nvars.} _numeric_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  retain _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
run;

data sort; 
  set want;
run;
proc sort data=sort;
  by descending key;
run;

data want(drop=i);
  set work.sort;
  array _n{&amp;amp;nvars.} _numeric_;
  array _rn{&amp;amp;nvars.} 8 _temporary_;/* variables for keeping data of previous observation */
  retain _rn:;
  do i=1 to dim(_n);
    if missing(_n{i}) then _n{i}=_rn{i};
    _rn{i}=_n{i};
  end;
run;

proc sort data=want out=want(drop=key);
  by key;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 05:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719531#M222802</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-16T05:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Filling up missing values with previous/next for time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719533#M222804</link>
      <description>&lt;P&gt;Thank you so much. This should be the accepted solution. I chose the previous one. This worked perfectly&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 05:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-up-missing-values-with-previous-next-for-time-series/m-p/719533#M222804</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2021-02-16T05:33:05Z</dc:date>
    </item>
  </channel>
</rss>

