DATA Step, Macro, Functions and more

How to create a dymanic URL variable within a SAS macro

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

How to create a dymanic URL variable within a SAS macro

I want to import a table from a url that has dynamic characters, but the code I have below is not working.  Is there a doable fix?

 

 

%let var1=2017;

%let var2='NC'; %let var9='Cary'; /

 

filename myfile url "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=&var1&task=0&state=&var2&place=&var9" lrecl=32767;

 

here is the original url::

filename myfile url "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=2017&task=0&state=NC&place=Cary" lrecl=32767;

 


Accepted Solutions
Solution
‎04-14-2017 02:11 PM
PROC Star
Posts: 307

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to tobyfarms

There is no need to quote your macro variables NC and Cary.

 

%let var9= Cary;

 

 

View solution in original post


All Replies
Solution
‎04-14-2017 02:11 PM
PROC Star
Posts: 307

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to tobyfarms

There is no need to quote your macro variables NC and Cary.

 

%let var9= Cary;

 

 

Contributor
Posts: 44

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to collinelliot
Awesome - I believe that resolved my issue (and error). Many Thanks!
Super User
Posts: 19,855

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to tobyfarms

@tobyfarms Please mark @collinelliot solution as correct.

Contributor
Posts: 44

Re: How to create a dymanic URL variable within a SAS macro

Thanks for letting me know. New to the process!
Contributor
Posts: 44

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to collinelliot
@collinelliot in my next data step, my place holders for city and step are not populating. where I would list:
state=&var2;
City=&var9;

Is there a fix for this also?
PROC Star
Posts: 307

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to tobyfarms

Yep, now you can quote them with double quotes:

 

state = "&var2"; etc.

Super User
Posts: 19,855

Re: How to create a dymanic URL variable within a SAS macro

Posted in reply to tobyfarms

@tobyfarms Remember that macro variables work as find/replace text but that the code resulting MUST be valid SAS syntax. In your current questions once you replace the macro variables your code is not valid. You can see this by easily replacing the macro variable in place and you see the errors.

 

First one:

 

%let var1=2017;

%let var2='NC'; %let var9='Cary'; /

 

filename myfile url "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=&var1&task=0&state=&var2&place=&var9" lrecl=32767;

 

Replaced (you can see the extra quotations in comparison to your posted URL):

filename myfile url "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=2017&task=0&state='NC'&place='Cary'" lrecl=32767;

 

Second one:

 

state=&var2;
City=&var9;

 

Replaced:

 

state=NC; -> this expects NC to be a variable since no quotations now. 
City=Cary;

 

Contributor
Posts: 44

Re: How to create a dymanic URL variable within a SAS macro

Yes, that makes sense.  However, when I do not use quotations or place VAR9, I reviece "." as the city & state columns of my results. 

 

The reason I am trying to use the macro is because I have to do this for multiple locations (which can vary in the future).

 

THANKS!!!

Spoiler
 
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 200 views
  • 4 likes
  • 3 in conversation