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.
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);
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.;
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.
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.
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";
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.
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);
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.