BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JackZ295
Pyrite | Level 9

Hi! I'm trying to concatenate a series of variables and text in a cats function in SAS, but for some reason, SAS will not recognize the last segment of my string "</a>" that I want to concatenate. My code is as follows: 

 

data four; 

set three; 

link_1="<a href=";
link_2="url";
link_3="&event_id=";
link_4="&page=";
link_a="</a>";

qi106=cats(link_1,'"',link_2, record_id, link_3, event_id, link_4, page,"'", link_a); 

run; 

 

When I try to print out the data set with just the link_a segment, e.g.: 

data four; 

set three;

link_a="</a>";

run; 

 

link_a just shows up as blank. Is there a reason why? Any input regarding this would be much appreciated! Thanks so much!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you don't have macro variables EVENT_ID then do reference it by having "&EVENT_ID" in your code.  Perhaps you meant to use generate a URL the uses EVENT_ID as a parameter value to pass into the program the interprets the link? Note that normally I have seen that URL's user ? for the first parameter and & for the following ones.  But check a few working URL's for your situation to be sure what syntax works.

 

If you want the macro processor to ignore the & then enclose that string in single quotes instead of double quotes.  

 

The <A> tag is not going to add much value to an actual user if there is nothing for them to click on to go to the target link.  If you don't have a more descriptive text then just repeat the URL so the user can see it on the page. Or perhaps just the variable parts of the link?

data four;
  set three;
  length url qi106 $300 ;
  url=cats('http://hostname/pagename'
         ,'?record_id=',record_id
         ,'&event_id=',revent_id
         ,'&page=',page
    );
  qi106=cats('<a href=',quote(trim(url)),'>',url,'</a>');
run;

 

View solution in original post

9 REPLIES 9
LinusH
Tourmaline | Level 20

Are you printing to an ODS HTML destination?

If so the tag won't visible in your report.

You should be able to confirm it's there by using a ODS LISTING.

What SAS client are you using?

Data never sleeps
JackZ295
Pyrite | Level 9

Hm, yes I'm trying to concatenate a string to create an HTML hyperlink. Is that possible within SAS? I'm using SAS 9.4. 

ballardw
Super User

You do realize that CATS code is creating a value with unbalanced quotes don't you?

qi106=cats(link_1,'"',link_2, record_id, link_3, event_id, link_4, page,"'", link_a); 

That means most processors for quoted values would expect Link_a to be within the not closed string starting with the double quote.

 

You might consider using single quotes in the Link_3 and Link_4 code. Currently SAS is expecting macro variables Event_id and Page to be defined. If you don't want to accidentally use such a macro variable if defined then use the

JackZ295
Pyrite | Level 9

Thanks! event_id and page are not macro variables. They are just variables that I want to include in the text string for the HTML url. I unfortunately have to piece together the URL because there are particular unique values for those variables. Any additional thoughts?

Tom
Super User Tom
Super User

We do not have your dataset THREE so we don't know what values it has for EVENT_ID, RECORD_ID or PAGE.

You also did not set values for the macro variable EVENT_ID or PAGE.

 

But if we make up some data we can then add a PUT statement to your data step so we can see what your function call generated.

80   data three;
81     event_id='event';
82     page='page';
83     record_id='record';
84   run;

NOTE: The data set WORK.THREE has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


85
86   %let event_id=macro_event;
87   %let page=macro_page;
88
89   data four;
90
91   set three;
92
93   link_1="<a href=";
94   link_2="url";
95   link_3="&event_id=";
96   link_4="&page=";
97   link_a="</a>";
98
99   qi106=cats(link_1,'"',link_2, record_id, link_3, event_id, link_4, page,"'", link_a);
100  put qi106=;
101  run;

qi106=<a href="urlrecordmacro_event=eventmacro_page=page'</a>

It does not help that you used a double quote at beginning of the generated URL and a single quote at the end.

Also the HTML is not valid.  You should have something like:

<a href="url_to_goto">Click here</a>

 

If the goal is to generate a quoted URL wouldn't it be much clearer to generate the URL and then use QUOTE() function to add the quotes?

 

JackZ295
Pyrite | Level 9

Also, a follow up question is if we would need to have a title for the hyperlink (where you wrote "Click Here"), or if we could omit that section of the HTML.

Tom
Super User Tom
Super User

If you don't have macro variables EVENT_ID then do reference it by having "&EVENT_ID" in your code.  Perhaps you meant to use generate a URL the uses EVENT_ID as a parameter value to pass into the program the interprets the link? Note that normally I have seen that URL's user ? for the first parameter and & for the following ones.  But check a few working URL's for your situation to be sure what syntax works.

 

If you want the macro processor to ignore the & then enclose that string in single quotes instead of double quotes.  

 

The <A> tag is not going to add much value to an actual user if there is nothing for them to click on to go to the target link.  If you don't have a more descriptive text then just repeat the URL so the user can see it on the page. Or perhaps just the variable parts of the link?

data four;
  set three;
  length url qi106 $300 ;
  url=cats('http://hostname/pagename'
         ,'?record_id=',record_id
         ,'&event_id=',revent_id
         ,'&page=',page
    );
  qi106=cats('<a href=',quote(trim(url)),'>',url,'</a>');
run;

 

JackZ295
Pyrite | Level 9

Thanks @Tom ! I did make the change to enclose the non-macro variables in single quotes. For some reason, the HTML URL hyperlink doesn't generate in the output. Does it generate for you? I know someone else had mentioned the ODS HTML, but I didn't know what they meant.

JackZ295
Pyrite | Level 9

Forgot to close the bracket; thanks @Tom !

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 9 replies
  • 1037 views
  • 1 like
  • 4 in conversation