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;
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:
Jim
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;
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:
Jim
@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?
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.