SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Oj1698
Calcite | Level 5

I have a data with a column of percentages, the column in inconsistent. Some obs are discrete values like X% and some are ranges x % - y%. I have to take average of range obs. How can I do that ? Below is an example of my problem

data _null_;

input pct $;

cards;

15%

5

30

10-15%

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

If below works for you will depend on your actual data.

I've made the assumption that all values are in percent.

data have;
  input pct $;
  cards;
15%
5
30
10-15%
;

data want(drop=_:);
  set have;
  length pct_n 8;
  pct=compress(pct,' %');
  _words=countw(pct,'-');
  do _i=1 to _words;
    pct_n=sum(pct_n,input(scan(pct,_i,'-'),best32.));
  end;
  pct_n=pct_n/(100*_words);
  format pct_n percent10.1;
run;

Patrick_0-1617431494425.png

 

View solution in original post

1 REPLY 1
Patrick
Opal | Level 21

If below works for you will depend on your actual data.

I've made the assumption that all values are in percent.

data have;
  input pct $;
  cards;
15%
5
30
10-15%
;

data want(drop=_:);
  set have;
  length pct_n 8;
  pct=compress(pct,' %');
  _words=countw(pct,'-');
  do _i=1 to _words;
    pct_n=sum(pct_n,input(scan(pct,_i,'-'),best32.));
  end;
  pct_n=pct_n/(100*_words);
  format pct_n percent10.1;
run;

Patrick_0-1617431494425.png

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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
  • 1 reply
  • 572 views
  • 2 likes
  • 2 in conversation