How do I merge values from a file into a series in memory?

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

How do I merge values from a file into a series in memory?

Unread post by macro »

I have a series in memory, and I have a file that contains the same series with a subsample of dates. I'm trying to merge the series in the file with the series already in memory, overwriting values in the original series that occur in the file, while leaving the rest of the series intact. Here's a simple example:

Code: Select all

calendar(q) 1940:1

* load original data
open data nipa.csv
    data(format = cdf, org = columns, dateform = "m/d/y") / gdp
close data

print 2013:1 2015:4 gdp

* merge new values
open data new_values.csv
    data(format = cdf, org = columns) / gdp
close data

print 2013:1 2015:4 gdp
This produces the output

Code: Select all

* <snipped> 

 ENTRY          GDP
 2013:01      15457.2
 2013:02      15500.2
 2013:03      15614.4
 2013:04      15761.5
 2014:01      15724.9
 2014:02      15901.5
 2014:03      16068.8
 2014:04      16151.4
 2015:01      16177.3
 2015:02      16270.4
 2015:03        NA
 2015:04        NA



* merge new value
open data new_values.csv
    data(format = cdf, org = columns) / gdp
close data

print 2013:1 2015:4 gdp


 ENTRY          GDP
 2013:01        NA
 2013:02        NA
 2013:03        NA
 2013:04        NA
 2014:01         -1
 2014:02        NA
 2014:03        NA
 2014:04        NA
 2015:01        NA
 2015:02         -2
 2015:03         -3
 2015:04        NA
whereas I'm trying to get the gdp series to look like this:

Code: Select all

 ENTRY          GDP
 2013:01      15457.2
 2013:02      15500.2
 2013:03      15614.4
 2013:04      15761.5
 2014:01      -1
 2014:02      15901.5
 2014:03      16068.8
 2014:04      16151.4
 2015:01      16177.3
 2015:02      -2
 2015:03      -3
 2015:04      -4
The dates and values in new_values.csv are completely arbitrary and aren't known ahead of time (except that they'll be in the same frequency as the series currently in memory). Is this possible?
Attachments
new_values.csv
(54 Bytes) Downloaded 842 times
nipa.csv
(4.65 KiB) Downloaded 819 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How do I merge values from a file into a series in memor

Unread post by TomDoan »

You can read the second series into a different name (or read the patch first and make a copy under a different name), then use %IF to patch over values that are not NA in the patch series.

Code: Select all

calendar(q) 1940:1

* load original data
open data nipa.csv
     data(format = cdf, org = columns, dateform = "m/d/y") / gdp
close data
 * merge new values
open data new_values.csv
   data(format = cdf,top=2, org = columns) / gdpnew
close data
set gdp = %if(%valid(gdpnew),gdpnew,gdp)
Post Reply