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
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.