<?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 Downloading dynamic list of weather data files from NCEI Land-based stations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/403342#M98010</link>
    <description>&lt;P&gt;** This is all public data. If this code gets to working, this script can become a resource for anyone who needs hourly weather details! **&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My goal&lt;/STRONG&gt;: create a process to automatically pull all NCEI Land-based station weather data files for 2017, for NC/SC stations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Version SAS 9.4M3, SAS Enterprise Guide, Windows local machine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;List of stations:&amp;nbsp;&lt;A href="ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.txt" target="_blank"&gt;ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.txt&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Hourly weather data folder:&amp;nbsp;&lt;A href="https://www.ncei.noaa.gov/data/global-hourly/access/2017/" target="_blank"&gt;https://www.ncei.noaa.gov/data/global-hourly/access/2017/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This pulls all stations in the US. It works fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;filename testurl ftp "isd-history.txt" cd="/pub/data/noaa/" host="ftp.ncdc.noaa.gov" user="anonymous";

data AUTORES.WEATHER_STATIONS;
infile testurl  FIRSTOBS=24; 
input USAF $ 1-6
	WBAN $ 8-12
	STATION_NAME $ 14-43
	CTRY $ 44-45
	ST $ 49-50
	CALL $ 52-55
	LAT 58-64
	LON 66-73
	ELEV 75-81
	BEGIN 83-90
	END 92-99
	;
	IF CTRY EQ "US";
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This creates a series of macro variables, "URL1" to "URL###", where ### is the number of stations to pull data for.&lt;/P&gt;&lt;P&gt;Variable values are the direct URL to the file to download.&lt;/P&gt;&lt;P&gt;This works as expected.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data _NULL_;
set AUTORES.WEATHER_STATIONS end=eof;
retain i;
if _n_ = 1 then i = 1;
if (ST EQ "NC" OR ST EQ "SC") AND END GT 20170000;
thisurl = cats("""https://www.ncei.noaa.gov/data/global-hourly/access/2017/", USAF, WBAN, ".csv""");
urlnum = cats("URL", i);
call symput(urlnum, thisurl); 
	strI = i;
	call symput("urlcount", left(put(strI, 8.)));
	i = i + 1;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's where it goes south: the macro loops as expected, but always only pulls the data for the LAST URL created (in this specific case, URL172).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO GET_WEATHER_STATION_READINGS;
%DO i = 1 %TO &amp;amp;urlcount; 
data WORK.NEW_WEATHER;
filename WFile url &amp;amp;&amp;amp;URL&amp;amp;urlcount debug; 

infile WFile DLM=',' DSD MISSOVER firstobs=2;

input 
	STATION : $20.
	DATE : E8601DT20.
	SOURCE : $8.
	LATITUDE : 10.6
	LONGITUDE : 10.6
	ELEVATION : 10.6
	NAME : $30.
	REPORT_TYPE : $10.
	CALL_SIGN : $10.
	QUALITY_CONTROL : $10.
	WND : $20.
	CIG : $20.
	VIS : $20.
	TMP : $20.
	DEW : $20.
	SLP : $20.
	AA1 : $20.
	AA2 : $20.
	AY1 : $20.
	AY2 : $20.
	GA1 : $20.
	GA2 : $20.
	GA3 : $20.
	GE1 : $20.
	GF1 : $20.
	MW1 : $20.
	REM : $20.
	EQD : $20.
	;
format DATE DATETIME20.;
run;

proc append base=AUTORES.WEATHER_HOURLY data=WORK.NEW_WEATHER;
run;

sleep(5, 1); 

%end;
%MEND GET_WEATHER_STATION_READINGS;


%GET_WEATHER_STATION_READINGS;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you may guess, i need it to pull URL1 - URL172, each one once.&lt;/P&gt;&lt;P&gt;It's a macro value issue, I suspect. I've got some basic understanding of macros, but I'm not sure how to change this into what I need.&lt;/P&gt;&lt;P&gt;(If the FILENAME command would accept a variable for a URL path, I doubt I'd need macros at all!!)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been fighting with this for three days, and solved a lot of other errors along the way. I humbly ask you please do not propose solutions without testing your code.&amp;nbsp; I do expect the final result file AUTORES.WEATHER_HOURLY to be a couple GB in size.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time.&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
    <pubDate>Wed, 11 Oct 2017 19:54:11 GMT</pubDate>
    <dc:creator>MarkPeskir</dc:creator>
    <dc:date>2017-10-11T19:54:11Z</dc:date>
    <item>
      <title>Downloading dynamic list of weather data files from NCEI Land-based stations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/403342#M98010</link>
      <description>&lt;P&gt;** This is all public data. If this code gets to working, this script can become a resource for anyone who needs hourly weather details! **&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My goal&lt;/STRONG&gt;: create a process to automatically pull all NCEI Land-based station weather data files for 2017, for NC/SC stations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Version SAS 9.4M3, SAS Enterprise Guide, Windows local machine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;List of stations:&amp;nbsp;&lt;A href="ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.txt" target="_blank"&gt;ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.txt&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Hourly weather data folder:&amp;nbsp;&lt;A href="https://www.ncei.noaa.gov/data/global-hourly/access/2017/" target="_blank"&gt;https://www.ncei.noaa.gov/data/global-hourly/access/2017/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This pulls all stations in the US. It works fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;filename testurl ftp "isd-history.txt" cd="/pub/data/noaa/" host="ftp.ncdc.noaa.gov" user="anonymous";

data AUTORES.WEATHER_STATIONS;
infile testurl  FIRSTOBS=24; 
input USAF $ 1-6
	WBAN $ 8-12
	STATION_NAME $ 14-43
	CTRY $ 44-45
	ST $ 49-50
	CALL $ 52-55
	LAT 58-64
	LON 66-73
	ELEV 75-81
	BEGIN 83-90
	END 92-99
	;
	IF CTRY EQ "US";
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This creates a series of macro variables, "URL1" to "URL###", where ### is the number of stations to pull data for.&lt;/P&gt;&lt;P&gt;Variable values are the direct URL to the file to download.&lt;/P&gt;&lt;P&gt;This works as expected.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data _NULL_;
set AUTORES.WEATHER_STATIONS end=eof;
retain i;
if _n_ = 1 then i = 1;
if (ST EQ "NC" OR ST EQ "SC") AND END GT 20170000;
thisurl = cats("""https://www.ncei.noaa.gov/data/global-hourly/access/2017/", USAF, WBAN, ".csv""");
urlnum = cats("URL", i);
call symput(urlnum, thisurl); 
	strI = i;
	call symput("urlcount", left(put(strI, 8.)));
	i = i + 1;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's where it goes south: the macro loops as expected, but always only pulls the data for the LAST URL created (in this specific case, URL172).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO GET_WEATHER_STATION_READINGS;
%DO i = 1 %TO &amp;amp;urlcount; 
data WORK.NEW_WEATHER;
filename WFile url &amp;amp;&amp;amp;URL&amp;amp;urlcount debug; 

infile WFile DLM=',' DSD MISSOVER firstobs=2;

input 
	STATION : $20.
	DATE : E8601DT20.
	SOURCE : $8.
	LATITUDE : 10.6
	LONGITUDE : 10.6
	ELEVATION : 10.6
	NAME : $30.
	REPORT_TYPE : $10.
	CALL_SIGN : $10.
	QUALITY_CONTROL : $10.
	WND : $20.
	CIG : $20.
	VIS : $20.
	TMP : $20.
	DEW : $20.
	SLP : $20.
	AA1 : $20.
	AA2 : $20.
	AY1 : $20.
	AY2 : $20.
	GA1 : $20.
	GA2 : $20.
	GA3 : $20.
	GE1 : $20.
	GF1 : $20.
	MW1 : $20.
	REM : $20.
	EQD : $20.
	;
format DATE DATETIME20.;
run;

proc append base=AUTORES.WEATHER_HOURLY data=WORK.NEW_WEATHER;
run;

sleep(5, 1); 

%end;
%MEND GET_WEATHER_STATION_READINGS;


%GET_WEATHER_STATION_READINGS;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you may guess, i need it to pull URL1 - URL172, each one once.&lt;/P&gt;&lt;P&gt;It's a macro value issue, I suspect. I've got some basic understanding of macros, but I'm not sure how to change this into what I need.&lt;/P&gt;&lt;P&gt;(If the FILENAME command would accept a variable for a URL path, I doubt I'd need macros at all!!)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been fighting with this for three days, and solved a lot of other errors along the way. I humbly ask you please do not propose solutions without testing your code.&amp;nbsp; I do expect the final result file AUTORES.WEATHER_HOURLY to be a couple GB in size.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time.&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 19:54:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/403342#M98010</guid>
      <dc:creator>MarkPeskir</dc:creator>
      <dc:date>2017-10-11T19:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Downloading dynamic list of weather data files from NCEI Land-based stations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/403350#M98012</link>
      <description>&lt;P&gt;Shouldn't it be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token statement"&gt;filename&lt;/SPAN&gt; WFile url &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;URL&lt;FONT color="#ff6600"&gt;&lt;SPAN class="token operator"&gt;&amp;amp;i&lt;/SPAN&gt;&lt;/FONT&gt; debug&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 20:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/403350#M98012</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-10-11T20:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Downloading dynamic list of weather data files from NCEI Land-based stations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/404514#M98328</link>
      <description>Ha, well, yes. Yes it should be.&lt;BR /&gt;So many other technical hurdles I jumped to reach this point, only to get tripped up on loop handling.&lt;BR /&gt;Thanks for your observant eye.</description>
      <pubDate>Mon, 16 Oct 2017 15:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Downloading-dynamic-list-of-weather-data-files-from-NCEI-Land/m-p/404514#M98328</guid>
      <dc:creator>MarkPeskir</dc:creator>
      <dc:date>2017-10-16T15:51:40Z</dc:date>
    </item>
  </channel>
</rss>

