Hi I have a column called ' Decimal' which has the required decimal places stored (It is in numeric) eg
Decimal Data
0 2.222
1 3.322
So I want the data to look like;
Data
2
3.3
I know the round function can achieve this easily but how do you use the variable 'Decimal' to convert the Data into its actual decimal places.
I tries this so far on Proc SQL
round(Data,Decimal) as NewData
also tried
strip(put(Data,8.Decimal)) as NewData
It seems like I cannot use a variable to manipulate the decimal places of another variable. Any help is appreciated!
Thanks
Format your posts as a self-contained data step using datalines statements.
Revisit the documentation on the round function.
Does this give you what you want?
data have;
length Decimal Data 8;
input Decimal Data;
datalines;
0 1.123
1 2.345
2 3.456
3 4.567
;
run;
data want;
set have;
newdata=round(Data,10**(-Decimal));
format newdata best.;
run;
Output:
Decimal Data newdata
0 1.123 1
1 2.345 2.3
2 3.456 3.46
3 4.567 4.567
You can definitely do this, but you need to use PUTC() or PUTN() which allow the second parameter to be a defined string. So you need the length and then the decimal place and combine them into the format you want and then pass that to the PUT function.
data want;
set have;
myformat = catx(".", "8", decimalPlaces);
myValue = left(putn(value, myformat));
run;
Results:
| Obs | decimalPlaces | value | myformat | myValue |
|---|---|---|---|---|
| 1 | 0 | 2.222 | 8.0 | 2 |
| 2 | 1 | 3.322 | 8.1 | 3.3 |
@kenjichan1212 wrote:
Hi I have a column called ' Decimal' which has the required decimal places stored (It is in numeric) eg
Decimal Data
0 2.222
1 3.322
So I want the data to look like;
Data
2
3.3
I know the round function can achieve this easily but how do you use the variable 'Decimal' to convert the Data into its actual decimal places.
I tries this so far on Proc SQL
round(Data,Decimal) as NewData
also tried
strip(put(Data,8.Decimal)) as NewData
It seems like I cannot use a variable to manipulate the decimal places of another variable. Any help is appreciated!
Thanks
Here is another somewhat clunky way that does not involve converting to character:
data want;
data_value = 2.222;
decimals = 2;
data_rounded = round(data_value, 1/(10**decimals));
put _all_;
run;
Format your posts as a self-contained data step using datalines statements.
Revisit the documentation on the round function.
Does this give you what you want?
data have;
length Decimal Data 8;
input Decimal Data;
datalines;
0 1.123
1 2.345
2 3.456
3 4.567
;
run;
data want;
set have;
newdata=round(Data,10**(-Decimal));
format newdata best.;
run;
Output:
Decimal Data newdata
0 1.123 1
1 2.345 2.3
2 3.456 3.46
3 4.567 4.567
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.