Hi all! i'm writing a macro function to retrieve data online from online
I tried
%macro access (url = , fname = )
%let url = "ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NAMCS/sas"
filename ed03 &url./&fname;
%mend access;
Thanks.
The syntax for a filename to reference an FTP site is
FILENAME fileref FTP 'external-file' <ftp-options>;
but all that generates is a link to the file. You would then have to write code to use that filename. such as either a data step or proc import or similar.
Did you have any code that worked before you attempted the macro? It sounds like you didn't. The approach to developing macro code, if needed, is to get one example working without any macro code involved and then identify the pieces that need to be modified.
You would likely have a better shot starting with
%macro access (url = , fname = ); %let url =ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NAMCS/sas ; filename ed03 ftp "&url./&fname"; %mend access;
When you get errors, which I am pretty sure you did when attempting to use the filename, run macro code with
OPTIONS MPRINT SYMBOLGEN;
before your macro call.
Then copy and paste the result from the log into a code box opened with forum {I} menu.
The mprint will show the code generated and symbolgen show the macro text resolutions.
Your initial code has no ; at the end of either the %macro or %let statements so the Filename portion was PART of URL. And your macro likely didn't even compile. I get:
1149 %macro access (url = , fname = ) 1150 %let url = "ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NAMCS/sas" ERROR: Expected semicolon not found. The macro will not be compiled. ERROR: A dummy macro will be compiled. 1151 1152 filename ed03 &url./&fname; 1153 1154 %mend access;
So of course any use of that macro will fail as in effect doing
%access (<parameters>) will be the equivalent of a null statement.
Which should have told you have syntax issues.
And I doubt if you are going to get anything from an exe file. If those are supposed to be self extracting data files then just download them and is point to the file in your browser and do a save target as or what ever.
OR learn the syntax for the FTP command. You could generate a X statement, or SYSTEM function or %SYSEXEC statement if you really must use SAS.
Don't have time to dig into this, but if the data is also available in their open data portal you can extract it very easily.
https://data.cdc.gov/browse?q=nchs&sortBy=relevance&anonymous=true
If it's not, I would consider asking them to do so.
Also, do you have XCMD enabled? If so, this is possible but you still download the file and extract it, but SAS does it instead of you. I highly suspect whoever made the recommendation would expect something along the lines of the first approach, using the API and a live connection.
The syntax for a filename to reference an FTP site is
FILENAME fileref FTP 'external-file' <ftp-options>;
but all that generates is a link to the file. You would then have to write code to use that filename. such as either a data step or proc import or similar.
Did you have any code that worked before you attempted the macro? It sounds like you didn't. The approach to developing macro code, if needed, is to get one example working without any macro code involved and then identify the pieces that need to be modified.
You would likely have a better shot starting with
%macro access (url = , fname = ); %let url =ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NAMCS/sas ; filename ed03 ftp "&url./&fname"; %mend access;
When you get errors, which I am pretty sure you did when attempting to use the filename, run macro code with
OPTIONS MPRINT SYMBOLGEN;
before your macro call.
Then copy and paste the result from the log into a code box opened with forum {I} menu.
The mprint will show the code generated and symbolgen show the macro text resolutions.
Your initial code has no ; at the end of either the %macro or %let statements so the Filename portion was PART of URL. And your macro likely didn't even compile. I get:
1149 %macro access (url = , fname = ) 1150 %let url = "ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NAMCS/sas" ERROR: Expected semicolon not found. The macro will not be compiled. ERROR: A dummy macro will be compiled. 1151 1152 filename ed03 &url./&fname; 1153 1154 %mend access;
So of course any use of that macro will fail as in effect doing
%access (<parameters>) will be the equivalent of a null statement.
Which should have told you have syntax issues.
And I doubt if you are going to get anything from an exe file. If those are supposed to be self extracting data files then just download them and is point to the file in your browser and do a save target as or what ever.
OR learn the syntax for the FTP command. You could generate a X statement, or SYSTEM function or %SYSEXEC statement if you really must use SAS.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.