How do I construct a url from input?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How do I construct a url from input?

[ Edited ]

I'm trying to build a url from user generated input(to access different information using the same code), but I'm having an interesting error with the string I build. The error is the same whether I use cats(args) or compress(args). When I hard-code the url it works perfectly. I'm using SAS 9.3.

 

Here's my code snippet:

%let nptyr1 = "2012";

%let nptyr2 = "2013";

%let nptqtr1 = "1";

%let nptqtr2 = "1";

%let indstry = "10";

 

data Temp0;

y1 = &nptyr1;

q1 = &nptqtr1;

y2 = &nptyr2;

q2 = &nptqtr2;

nn = &indstry;

y3 = compress("http://www.bls.gov/cew/data/api/"||y1||"/"||q1||"/industry/"||nn||".csv");

call symput('z1', y5);

y4 = compress("http://www.bls.gov/cew/data/api/"||y2||"/"||q2||"/industry/"||nn||".csv");

call symput('z2', y4);

run;

/************************************************************************/

/* Download data */

/************************************************************************/

/* Set filenames*/

filename website1 url &z1;

filename copy1 'C:\temp\file1.csv';

filename website2 url &z2;

filename copy2 'C:\temp\file2.csv';

 

and this is the error I get:

 

ERROR: Error in the FILENAME statement.

NOTE: Line generated by the macro variable "Z1".

1 http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

--

23

ERROR 23-2: Invalid option name //.

1 ! http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

---

23

ERROR 23-2: Invalid option name cew.

1 ! http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

----

23

ERROR 23-2: Invalid option name data.

1 ! http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

---

23

ERROR 23-2: Invalid option name api.

1 ! http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

--------

23

ERROR 23-2: Invalid option name industry.

1 ! http://www.bls.gov/cew/data/api/2012/1/industry/10.csv

---

23

ERROR 23-2: Invalid option name csv.

 


Accepted Solutions
Solution
‎04-25-2016 02:43 PM
Super User
Super User
Posts: 6,845

Re: How do I construct a url from input?

The FILENAME statement needs a quoted string.  You might want to add the quotes in the data step since some URLs can include & and that can confuse SAS.  Surrounding the values with single quotes will prevent SAS from treating any & in the URL as macro triggers.

 

y3 = cats("'"
         ,catx('/'
              ,'http://www.bls.gov/cew/data/api'
              ,y1
              ,q1
              ,'industry'
              ,cats(nn,'.csv')
              )
         ,"'"
         )
;
call symputX('z1', y3);

View solution in original post


All Replies
Super User
Posts: 19,167

Re: How do I construct a url from input?

call symput('z1', y5);

That should be y3, not y5?

 

%let nptyr1 = 2012;
%let nptyr2 = 2013;
%let nptqtr1 = 1;
%let nptqtr2 = 1;
%let indstry = 10;
 
data Temp0;
y1 = &nptyr1;
q1 = &nptqtr1;
y2 = &nptyr2;
q2 = &nptqtr2;
nn = &indstry;
y3 = catt('http://www.bls.gov/cew/data/api/',y1,'/',q1,'/industry/',nn,'.csv');
call symputx('z1', y3);
y4 = catt('http://www.bls.gov/cew/data/api/',y2,'/',q2,'/industry/',nn,'.csv');
call symput('z2', y4);
run;

%put &z1;
%put &z2.;
Super User
Posts: 19,167

Re: How do I construct a url from input?

Also, you probably don't need to assign the values to a variable, you should be able to use the variables directly in the catt function.

New Contributor
Posts: 3

Re: How do I construct a url from input?

The %put statment prints the appropriate url, but when this is passed into a filename statement the same error occurs. It does look like more efficient code.

Super User
Posts: 5,367

Re: How do I construct a url from input?

The value of Y4 does not include quotation marks.  You may have the correct syntax to construct the URL, but you still need to add the quotes in the FILENAME statement:

 

filename website2 url "&z2";

 

New Contributor
Posts: 3

Re: How do I construct a url from input?

Good catch on the y5 y3 thing. Thanks. I was trying a different thing where I called it a url in the data step, and that didn't work either.

Solution
‎04-25-2016 02:43 PM
Super User
Super User
Posts: 6,845

Re: How do I construct a url from input?

The FILENAME statement needs a quoted string.  You might want to add the quotes in the data step since some URLs can include & and that can confuse SAS.  Surrounding the values with single quotes will prevent SAS from treating any & in the URL as macro triggers.

 

y3 = cats("'"
         ,catx('/'
              ,'http://www.bls.gov/cew/data/api'
              ,y1
              ,q1
              ,'industry'
              ,cats(nn,'.csv')
              )
         ,"'"
         )
;
call symputX('z1', y3);
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 409 views
  • 1 like
  • 4 in conversation