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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 557 views
  • 3 likes
  • 4 in conversation