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

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

          

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

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

Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

3 REPLIES 3
Reeza
Super User

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

          


 

SASKiwi
PROC Star

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;
ScottBass
Rhodochrosite | Level 12

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

Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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
  • 3 replies
  • 1079 views
  • 3 likes
  • 4 in conversation