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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
Reeza
Super User
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.;
Reeza
Super User

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.

ajffja
Calcite | Level 5

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.

Astounding
PROC Star

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

 

ajffja
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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