BookmarkSubscribeRSS Feed
SASuserlot
Barite | Level 11

 


data dec;
input catn score1 score2 ;
cards;
 1   7.4245  4.123  
 3   6.4243  4.243
 7   4.7653  7.653
 11   5.2556  2.556
 6   5.3411  3.411
 8   5.0046  0.046
 7   3.9589  9.589
99   5.1905  1.905
;
run;
data dec1;
set dec;
array x(2) score1 score2;
	do i = 1 to 2 by 1;
	 x(i) = round(x(i),0.01);
	end;
	drop i;
run;

 I would like to know how I can achieve the same results using the Numeric formats like 6.2 or 8.2 formats.

Also is it possible dynamically control the digits after decimal based on my requirement.

for example I want to round/format the data one digit after decimal less than the actual data?

Thank you for your suggestions.

 

7 REPLIES 7
PaigeMiller
Diamond | Level 26

I would like to know how I can achieve the same results using the Numeric formats like 6.2 or 8.2 formats.

 

format score1 8.4 score2 7.3;

 

Also is it possible dynamically control the digits after decimal based on my requirement. 

for example I want to round/format the data one digit after decimal less than the actual data?

 

Please give example of the desired output.

--
Paige Miller
SASuserlot
Barite | Level 11

like 

score1 score2
7.4245  4.123

First observation have 4 digits after decimal . I want dynamically identify how many digits available after decimal in score1 and score 2 instead of looking into the data separately  . Then output the the values one digits less than that after decimal.

ex: 

score1 have 7.4245---> want 7.424 

score2 have 4.123---> want 4.12

Thanks

PaigeMiller
Diamond | Level 26

I have a lot of questions here.

 

  1. Do you want the program to figure this out how many decimal places to use, or do you just tell the program that SCORE1 should be rounded to 3 digits???
  2. Are these values numeric, or character? If numeric, are there actually more digits beyond what is shown because the formatting limits the decimals to 4 places?
  3. You say "I want to round/format the data" but format is not the same as round, they do different things, which do you really want?
  4. Why are you doing this? What use will be made of the results?

 

Please answer all 4 questions.

--
Paige Miller
SASuserlot
Barite | Level 11
1. Program to figure out the how many decimals need to be in output based on the data.
2. These are numeric. At this moment that only the digits I have after the decimals . This was like standard scores we get from certain study. However I will try this to apply for different dataset based on the maximum digits present.
3. Sorry for confusion I mean to to say round the value to 3 digits if it has the 4 digits after decimal. I just used round option in my code to show how I want.
4. Because We need to display the results based on the score type and digits present in it. for example if the data have the
3 digits after decimal , then we need to display mean up to 2 digits and SD 1 digits and may be vice versa.

Hope I answered the questions. If not please let me know. Thanks
PaigeMiller
Diamond | Level 26

So starting with #4, you want some formatting on a report. Are there so many variables (your example has just 2 but I am assuming the real world problem has more) that you need the SAS program to figure it out? Because, the simplest solution to me would be to have a human say column 1 needs 4 digits, and column 2 needs 3 digits and so on. To be honest, since you originally asked the question, I have tried to think of a way to have the computer figure out how many places after the decimal there are, and I have decided its either difficult or impossible (although I'm sure someone will come along with an ingenious solution) and simply much easier for a human to look at the data and determine digits than for some human to write code for this.

--
Paige Miller
SASuserlot
Barite | Level 11

Thanks you so much your answer.  I really appreciate for your time.

pink_poodle
Barite | Level 11

Yes, you can make a dynamic format. For example, you have a number 7.4245. It uses six spaces including one for the decimal point and has 4 digits after the decimal point. Therefore, the format that would fit that number perfectly is 6.4. If you want to round it dynamically to one digit less than the last where the last digit can be any location, like 7.42455555…, you would need to specify the format &mvar_a.&mvar_b using variables a and b and putting them into macro variables mvar_a and mvar_b:

a =  length(number)

b = length(substring(number, index(‘.’)) minus however many digits you would like to hide

Call symputx(mvar_a, “a”);

Call symputx(mvar_b, “b”);

 

 

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
  • 7 replies
  • 576 views
  • 0 likes
  • 3 in conversation