BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Viveme789
Fluorite | Level 6

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
Reeza
Super User

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.

 

 

ballardw
Super User

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 906 views
  • 0 likes
  • 3 in conversation