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

Hello, I’d like to know if anyone knows how I can replace a double hyphen (“--”) with an EN DASH ("–"). For reference, in microsoft word, this character can be typed using Alt + 0150.

Please, see the example below of what I’m trying to do. I can not use a simple hyphen ("-") as a solution, this restriction was imposed.


data have;
length region $50;
input region $char50.;
datalines;
Baden--Wurttemberg
Saxony--Anhalt
North Rhine--Westphalia
;
run;


data want;
set have;

region_en_dash = tranwrd (region, "--", "EN_DASH");

run;



Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

Hi:

  I am not sure you'll ever see the change in the data viewer in EG. However, based on this example https://documentation.sas.com/doc/en/statug/15.2/statug_kaplan_sect045.htm in the documentation about how to get a large dash in PROC LIFETEST, I used the ODS ESCAPECHAR string (*ESC*){Unicode '2014'x} that they show and the results seemed to work for me in ODS output. Basically, I changed your code to use the ODS ESCAPECHAR string in the TRANWRD function and then used PROC PRINT to send the output to either the default destination or ODS PDF or ODS RTF.

PDF results:

Cynthia_sas_1-1753991219846.jpeg

 

RTF results:

Cynthia_sas_2-1753991248974.jpeg

 

But, when I look at the data in the data viewer in SAS Studio, I see the actual ESCAPECHAR string, which will only be rendered in ODS output, not in the data viewer.

 

Cynthia

 

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

En-dash is hex 96 (see https://www.ascii-code.com/CP1250) ... so you want

 

region_en_dash = tranwrd (region, "--", "96"x);

 

--
Paige Miller
ballardw
Super User

See if this meets your need:

 

data want;
set have;
region_en_dash = tranwrd (region, "--", byte(150));

run;

The BYTE function returns characters from ASCII or EBCDIC coding, depending on OS and emulates the characters on the keypad mentioned.

 

For a more general approach you may need to look at UNICODE characters.

MFraga
Quartz | Level 8

Thanks for your help. Unfortunately, none of the solutions worked, the character simply disappears in my SAS interface (in other words, the words get stuck together without the en dash or any space in between). How can I understand why this is happening and how to fix it?

PaigeMiller
Diamond | Level 26

Saying "none of the solutions worked" gives us no information to help figure out what the problem is. We need at least these items of information: What is your SAS interface? Show us the code you are using. Show us a screen capture of the result where the en dash has disappeared. What language do you have SAS set to?

--
Paige Miller
MFraga
Quartz | Level 8

SAS Entreprise Guide
The code I am using:

data want_hex;
set have;

region_en_dash = tranwrd (region, "--", "96"x);

run;

data want_byte;
set have;

region_en_dash = tranwrd (region, "--", byte(150));

run;


data want_KCVT;
set have;
en_dash = byte(150);
region_en_dash = tranwrd(region,'--',kcvt('E28093'x,'utf-8',getoption('encoding')));
run;


Encoding: LATIN9

Print screen attached showing the results.

Byte.png

Hex.png

kcvt.png

   

Tom
Super User Tom
Super User

That character does not exist in the LATIN9 encoding.

25   data test;
26     length wlatin1 latin9 $1 utf8 $4 ;
27     wlatin1='96'x;
28     utf8 = kcvt(wlatin1,'wlatin1','utf-8');
29     latin9 = kcvt(utf8,'utf-8','latin9');
30     put (_all_) (=$hex.);
31   run;

wlatin1=96 latin9=1A utf8=E2809320

The character '1A'x is what KCVT() returns for something that it cannot transcode.

MFraga
Quartz | Level 8

So what would be the way to work around this limitation? Changing the SAS encoding?

Cynthia_sas
Diamond | Level 26

Hi:

  I am not sure you'll ever see the change in the data viewer in EG. However, based on this example https://documentation.sas.com/doc/en/statug/15.2/statug_kaplan_sect045.htm in the documentation about how to get a large dash in PROC LIFETEST, I used the ODS ESCAPECHAR string (*ESC*){Unicode '2014'x} that they show and the results seemed to work for me in ODS output. Basically, I changed your code to use the ODS ESCAPECHAR string in the TRANWRD function and then used PROC PRINT to send the output to either the default destination or ODS PDF or ODS RTF.

PDF results:

Cynthia_sas_1-1753991219846.jpeg

 

RTF results:

Cynthia_sas_2-1753991248974.jpeg

 

But, when I look at the data in the data viewer in SAS Studio, I see the actual ESCAPECHAR string, which will only be rendered in ODS output, not in the data viewer.

 

Cynthia

 

PaigeMiller
Diamond | Level 26

@Cynthia_sas wrote:

Hi:

  I am not sure you'll ever see the change in the data viewer in EG. However, based on this example https://documentation.sas.com/doc/en/statug/15.2/statug_kaplan_sect045.htm in the documentation about how to get a large dash in PROC LIFETEST, I used the ODS ESCAPECHAR string (*ESC*){Unicode '2014'x} that they show and the results seemed to work for me in ODS output. Basically, I changed your code to use the ODS ESCAPECHAR string in the TRANWRD function and then used PROC PRINT to send the output to either the default destination or ODS PDF or ODS RTF.

 


Just pointing out that is an EM dash. Unicode 2013 is the EN dash.

--
Paige Miller
MFraga
Quartz | Level 8

Thank you all for your help! In fact, @Cynthia_sas , by applying your solution, I’m not able to get the data to display in the SAS interface the way I want. However, when I generate the results for export (which is what really matters to me now), I do get what I need. Thank you, @PaigeMiller , for pointing out the correct code for the EN DASH.

What would be the best way to have the EN DASH display correctly in my SAS interface? Would changing the encoding to UTF-8 be the solution?

Tom
Super User Tom
Super User

@MFraga wrote:

Thanks for your help. Unfortunately, none of the solutions worked, the character simply disappears in my SAS interface (in other words, the words get stuck together without the en dash or any space in between). How can I understand why this is happening and how to fix it?


What encoding are you using?

Check the value of the system option ENCODING.

%put %sysfunc(getoption(encoding));

You might try using KCVT() so that you can try making the en-dash character (if it exits) in your current encoding. 

In UTF-8 encoding and EN DASH is this character.

'E28093'x

So try

region_en_dash = tranwrd(region,'--',kcvt('E28093'x,'utf-8',getoption('encoding')));

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 11 replies
  • 1079 views
  • 10 likes
  • 5 in conversation