DATA Step, Macro, Functions and more

Access data online especially with .exe file that needs compressing.

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 18
Accepted Solution

Access data online especially with .exe file that needs compressing.

[ Edited ]

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. 

 


Accepted Solutions
Solution
‎01-12-2018 05:42 PM
Super User
Posts: 13,084

Re: Access data online especially with .exe file that needs compressing.

[ Edited ]
Posted in reply to Viveme789

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


All Replies
Super User
Posts: 22,874

Re: Access data online especially with .exe file that needs compressing.

Posted in reply to Viveme789

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.

 

 

Solution
‎01-12-2018 05:42 PM
Super User
Posts: 13,084

Re: Access data online especially with .exe file that needs compressing.

[ Edited ]
Posted in reply to Viveme789

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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