BookmarkSubscribeRSS Feed
geneshackman
Pyrite | Level 9

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

6 REPLIES 6
HB
Barite | Level 11 HB
Barite | Level 11

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. 

geneshackman
Pyrite | Level 9
As you say, I may have to format the decimal place in the programming part that's about the presentation. Okay, thanks.
ballardw
Super User

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.

SK_11
Obsidian | Level 7

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
Patrick
Opal | Level 21

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;

Patrick_0-1671097598746.png

 

Patrick
Opal | Level 21

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;

Patrick_0-1671098904787.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 6 replies
  • 635 views
  • 2 likes
  • 5 in conversation