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

Hi,

 

I have a dataset with some GPS positions including a dot which I want to change to a comma.

 

An example of the data is:

 

X                    Y

45.12542        12.07830

45.58219        11.98459

 

I have tried:

 

data want;
set have;
X=translate(X,'.',',');
Y=translate(Y,'.',',');
run;

But it only returns "missing" values, so all coordinates disappear. Both variables are of course numeric.

 

Thanks in advance! 

 

All best

MM

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Mikkel_madse wrote:

... Both variables are of course numeric.

...


Numbers contain neither commas or periods, so there is nothing to translate from/to.

To change how the values are displayed you just need to change the format used to display them.  The COMMAX format will display the decimal point as a comma instead of period.

format x y commax10.5 ;

 

View solution in original post

5 REPLIES 5
Cynthia_sas
SAS Super FREQ

Hi:
Remember that TRANSLATE is a little bit different. The first argument is the variable you want to change and the second argument is the new character to use and the third argument is the character to translate, like this:
newvar = translate(oldvar,newval,OLDval);

So if you want to CHANGE the . to a , then you'd have:
newvar=translate(oldvar,',','.');
with the comma listed as the second argument. However, also note that if your X and Y are numeric, you may receive a NOTE in the log that numeric values have been converted to character. So if you do THIS:
x=translate(x,',','.');
then X will effectively be missing after your statement runs, even if you fix the translate function because X has to be character for translate to work and if your X is numeric, the result of the translate function will be assigning a missing value to X.

If all you want to do is display the values with a comma instead of a period, you can do that with the COMMAX format. but if you want to create a new variable, it will have to be character.

Here is an example to try:

data fakedata;
  infile datalines dlm=',';
  input examp $ numX numY;

  ** create character variables with comma using PUT function;
  charx = put(numX,commax9.5);
  chary = put(numY,commax9.5);

  ** save orig values because they will be missing after translate;
  savenumx = numx;
  savenumy = numy;

  ** now do translate using correct syntax ;
  ** (but numx and numy are numeric so they will be missing after translate);
  numX=translate(numX,',','.');
  numY=translate(numY,',','.');
return;
datalines;
a1,45.12542,12.07830
b2,45.58219,11.98459
;
run;
title;

proc print data=fakedata;
  title 'Display original numbers with commas without using TRANSLATE';
  var examp savenumx savenumy;
  format savenumx savenumy commax9.5;
run;

proc contents data=fakedata;
  title 'check variables type -- new variables are character';
run;

proc print data=fakedata;
  title 'Original numbers show period, character versions show comma';
format savenumx savenumy 9.5;
run;



Cynthia

Tom
Super User Tom
Super User

@Mikkel_madse wrote:

... Both variables are of course numeric.

...


Numbers contain neither commas or periods, so there is nothing to translate from/to.

To change how the values are displayed you just need to change the format used to display them.  The COMMAX format will display the decimal point as a comma instead of period.

format x y commax10.5 ;

 

Reeza
Super User
If it's already numeric, then you want to change the appearance of the period to a comma - unless you need to plot or map it somehow. But you can change the format instead of changing the value, as Tom indicates.
Mikkel_madsen
Obsidian | Level 7

Thank you, everyone! 

I need to replace the dot with a comma before import to QGIS for geo-analysis. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 6790 views
  • 3 likes
  • 5 in conversation