I hope this is a simple question. I have the following data. The first five rows are population, the second five rows are percent of population. For reasons too complex to get into here, I need them both in the same "rate" column, so please, no suggestions about putting them in different columns.
My question is how to save a sas data set where population has no decimal places and percent is rounded to one decimal place.
group | rate |
A | 35000 |
B | 746 |
C | 462158 |
D | 9534 |
E | 760449 |
A | 31.523 |
B | 30.64 |
C | 12.701 |
D | 24.339 |
E | 18.9111 |
Thanks
Gene
A numeric variable can only have one format.
Don't worry about how it is stored, format in the presentation. Surely there is a key or indicator to tell you whether the value in rate is a population or a percent. Perhaps if there are no populations under 100 you could do it that way.
Generic comment: Poor things happen when a variable has more than one meaning, ie. Count of population, percentage of anything.
If you are creating output files that go into another application then that's possibly poor design in that program but I would likely wait until writing the output file to worry about the format and not try to use it in SAS.
If someone is manipulating this data in SAS then they have chosen something that is going to require lots of work to deal with properly.
data table;
input group $ rate ;
datalines;
A 35000
B 746
C 462158
D 9534
E 760449
A 31.523
B 30.64
C 12.701
D 24.339
E 18.9111
;
run;
data want;
set table;
if scan(put(rate, best.),2,".") = '' then rate = put(rate, 12.);
if scan(put(rate, best.),2,".") ne '' then rate = put(rate, 12.1);
run;
Output:
group | rate |
A | 35000 |
B | 746 |
C | 462158 |
D | 9534 |
E | 760449 |
A | 31.5 |
B | 30.6 |
C | 12.7 |
D | 24.3 |
E | 18.9 |
Not perfect but one of below two options might give you what you're after.
data have;
input group $ rate;
datalines;
A 35000
B 746
C 462158
D 9534
E 760449
A 31.523
B 30.64
C 12.701
D 24.339
E 18.9111
;
proc format;
value real_pct
-100 - 100 = [16.1]
other = [16.]
;
run;
data want;
set have;
format rate2 real_pct.;
rate2=rate;
format rate3 best32.;
rate3=round(rate,0.1);
run;
proc print data=want;
run;
Or here a 3rd option.
data have;
input group $ rate;
datalines;
A 35000
B 746
C 462158
D 9534
E 760449
A 31.523
B 30.64
C 12.701
D 24.339
E 18.9111
;
proc fcmp outlib=work.fcmp.funcs;
function f_real_pct(in) $16;
if ceil(in)=in then
out=put(in,f16.);
else
out=catx(' ',put(in,f16.1),'%');
out=strip(out);
return(out);
endsub;
run;quit;
options cmplib=work.fcmp;
proc format;
value real_pct
other=[f_real_pct()]
;
run;
proc print data=have;
format rate real_pct.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.