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

Hi,

 

How do I format my values to get the # of decimal places I want?

 

data:

col1                                 col2

       Mean (SD)

168.4932 (9.9923)

       Median

167.5122

       IQR

160.523, 178.833

       Min, Max

151.623, 185.423

 

 

want:

       Mean (SD)

168.49 (9.992)

       Median

167.51

       IQR

160.52, 178.83

       Min, Max

151.6, 185.4

 

 if nmiss(mean,sd)=0 then do;
	 	ord=2;
		val=strip(put(mean,4.1))||' ('||strip(put(sd,5.2))||')';
		output;
	end;
	if median ne . then do;
		ord=3;
		val=strip(put(median,4.1));
		output;
	end;
	if n(q1,q3)=2 then do; 
		ord=4;
		val=strip(put(q1,4.1))||', '||strip(put(q3,4.1)) ;
		output;
	end;
	if n(min,max)=2 then do;
		ord=5;
		val=strip(put(min,4.1))||', '||strip(put(max,4.1)) ;
		output;
	end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

You'll want to allow for, in your format, the total length of the full number of the characters you want in your final form.

 

Here's a sample of code with formats that will give you want you want.

DATA	Have;
	INFILE	DATALINES	MISSOVER	DSD	DLM='09'X;
	INPUT
		Mean
		SD
		Median
		q1
		q3
		min
		max
		;
DATALINES;
168.4932	9.9923	167.5122	160.523	178.833	151.623	185.423
RUN;

DATA	Want;
	KEEP	Description	Val;
	SET		Have;

	Description	=	'Mean (SD)';
	if nmiss(mean,sd)=0 then do;
	 	ord=2;
		val=strip(put(mean,6.2))||' ('||strip(put(sd,5.3))||')';
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'Median';
	if median ne . then do;
		ord=3;
		val=strip(put(median,6.2));
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'IQR';
	if n(q1,q3)=2 then do; 
		ord=4;
		val=strip(put(q1,6.2))||', '||strip(put(q3,6.2)) ;
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'Min, Max';
	if n(min,max)=2 then do;
		ord=5;
		val=strip(put(min,5.1))||', '||strip(put(max,5.1)) ;
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	PUT	'NOTE-  ';
run;

PROC	PRINT	DATA=Want;
RUN;

Results:

jimbarbour_0-1627015312911.png

 

Jim

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

The data does not match the text. Like this?

 

if nmiss(mean,sd)=0 then do;
	 	ord=2;
		val=strip(put(mean,9.2))||' ('||strip(put(sd,9.3))||')';
		output;
	end;
if median ne . then do;
		ord=3;
		val=strip(put(median,49.2));
		output;
	end;
if n(q1,q3)=2 then do; 
		ord=4;
		val=strip(put(q1,9.2))||', '||strip(put(q3,9.2)) ;
		output;
	end;
if n(min,max)=2 then do;
		ord=5;
		val=strip(put(min,9.1))||', '||strip(put(max,9.1)) ;
		output;
	end;
run;

 

 

 

 

jimbarbour
Meteorite | Level 14

You'll want to allow for, in your format, the total length of the full number of the characters you want in your final form.

 

Here's a sample of code with formats that will give you want you want.

DATA	Have;
	INFILE	DATALINES	MISSOVER	DSD	DLM='09'X;
	INPUT
		Mean
		SD
		Median
		q1
		q3
		min
		max
		;
DATALINES;
168.4932	9.9923	167.5122	160.523	178.833	151.623	185.423
RUN;

DATA	Want;
	KEEP	Description	Val;
	SET		Have;

	Description	=	'Mean (SD)';
	if nmiss(mean,sd)=0 then do;
	 	ord=2;
		val=strip(put(mean,6.2))||' ('||strip(put(sd,5.3))||')';
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'Median';
	if median ne . then do;
		ord=3;
		val=strip(put(median,6.2));
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'IQR';
	if n(q1,q3)=2 then do; 
		ord=4;
		val=strip(put(q1,6.2))||', '||strip(put(q3,6.2)) ;
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	Description	=	'Min, Max';
	if n(min,max)=2 then do;
		ord=5;
		val=strip(put(min,5.1))||', '||strip(put(max,5.1)) ;
		output;
	end;
	PUT	'NOTE-  ' Description	val;

	PUT	'NOTE-  ';
run;

PROC	PRINT	DATA=Want;
RUN;

Results:

jimbarbour_0-1627015312911.png

 

Jim

 

ballardw
Super User

@HitmonTran wrote:

Hi,

 

How do I format my values to get the # of decimal places I want?

 

data:

col1                                 col2

       Mean (SD)

168.4932 (9.9923)

       Median

167.5122

       IQR

160.523, 178.833

       Min, Max

151.623, 185.423

 

 

want:

       Mean (SD)

168.49 (9.992)

       Median

167.51

       IQR

160.52, 178.83

       Min, Max

151.6, 185.4

 

 if nmiss(mean,sd)=0 then do;
	 	ord=2;
		val=strip(put(mean,4.1))||' ('||strip(put(sd,5.2))||')';
		output;
	end;
	if median ne . then do;
		ord=3;
		val=strip(put(median,4.1));
		output;
	end;
	if n(q1,q3)=2 then do; 
		ord=4;
		val=strip(put(q1,4.1))||', '||strip(put(q3,4.1)) ;
		output;
	end;
	if n(min,max)=2 then do;
		ord=5;
		val=strip(put(min,4.1))||', '||strip(put(max,4.1)) ;
		output;
	end;
run;

 


And how many decimals do you want? For which value. Not stated in your request. Your variables in the displayed "want", if I understand are Character, so there really aren't any decimals in a "format".

If you use the CATX function as in : val = catx(',',put(min,4.1),put(max,4.1)); you'll like be happier with results in the long run. 

Are you using the 4.1 without understanding that means 4 print positions with 1 decimal place?

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1210 views
  • 0 likes
  • 4 in conversation