Hello all,
I have a data set with a temperature variable in Celsius. For some values the decimal point is a comma instead, so I'll need to convert them to a period to run other codes. I could do a simple "if Temp = 37,0 then Temp = 37.0" but it's a large database I'd like to automate this at the start.
Sample data set below:
Data Work.Have;
Input PatientId $ AgeMo Temp $;
Datalines;
0001 11 37.9
0002 9 38.7
0003 44 38,1
0004 30 37.7
0005 19 37,5
;
Run;
Thanks in advance
Do you have other text in that field? Do you ever have values large enough that they also have separators between thousands and hundreds?
If not then just replace the comma with period.
temp=translate(temp,'.',',');
If you also have values like 1.234,56 then you could test if the last one is a comma and only then reverse them.
if char(temp,length(temp)-2)=',' then temp=translate(temp,',.','.,');
Do you have other text in that field? Do you ever have values large enough that they also have separators between thousands and hundreds?
If not then just replace the comma with period.
temp=translate(temp,'.',',');
If you also have values like 1.234,56 then you could test if the last one is a comma and only then reverse them.
if char(temp,length(temp)-2)=',' then temp=translate(temp,',.','.,');
Data Work.Have;
Input PatientId $ AgeMo Temp $;
Temp_n = input(translate(Temp,",","."), comma8.1);
drop Temp;
rename Temp_n = Temp;
Datalines;
0001 11 37.9
0002 9 38.7
0003 44 38,1
0004 30 37.7
0005 19 37,5
;
Run;
Thanks for the reply, this fixed the comma issue but now there's a new one. Variables without any decimals are assigned a period (40 -> 4.0). I didn't include those without decimals before, below is an updated code
Data Work.Have;
Input PatientId $ AgeMo Temp $;
Temp_n = input(translate(Temp,",","."), comma8.1);
Drop Temp;
Rename Temp_n = Temp;
Datalines;
0001 11 37.9
0002 9 38.7
0003 44 38,1
0004 30 37.7
0005 19 37,5
0006 20 40
0007 10 39
;
Run;
Which gives this output
0001 | 11 | 37.9 |
0002 | 9 | 38.7 |
0003 | 44 | 38.1 |
0004 | 30 | 37.7 |
0005 | 19 | 37.5 |
0006 | 20 | 4.0 |
0007 | 10 | 3.9 |
You do not want to include decimal width in the informat specification, unless you want SAS to divide text without the decimal point by that power of ten. Do not translate the periods to commas (unless perhaps your SAS session is one where that is what the COMMA informat wants).
data want;
input PatientId $ AgeMo Temp $;
Temp_n = input(translate(Temp,".",","), comma8.);
drop Temp;
rename Temp_n = Temp;
datalines;
0001 11 37.9
0002 9 38.7
0003 44 38,1
0004 30 37.7
0005 19 37,5
0006 20 40
0007 10 39
;
Patient Age Obs Id Mo Temp 1 0001 11 37.9 2 0002 9 38.7 3 0003 44 38.1 4 0004 30 37.7 5 0005 19 37.5 6 0006 20 40.0 7 0007 10 39.0
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.