BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ayooo1
Obsidian | Level 7

How can I concatenate the count and percent into this form: xxx(xxx.x%) and round the percent to one decimal?

example data:

data CP; 

input Treatment$ SEX$ COUNT PERCENT;

H1 Male 2 22.22%

H1 Female 7 77.77%

H2 Male 4 36.36%

H2 Female 7 63.63%

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So if you have actual percentages (so 0.2222 instead of 22.22 )

data CP; 
  input Treatment $ SEX $ COUNT PERCENT :percent.;
cards;
H1 Male 2 22.22%
H1 Female 7 77.77%
H2 Male 4 36.36%
H2 Female 7 63.63%
;

You could use the fact that the PERCENT format adds () around negative values to make it easier.

data want;
  set cp;
  length string $20;
  string=put(count,comma12.)||' '|| put(-percent,percent7.1);
run;
Obs    Treatment     SEX      COUNT    PERCENT     string

 1        H1        Male        2       0.2222    2 (22.2%)
 2        H1        Female      7       0.7777    7 (77.8%)
 3        H2        Male        4       0.3636    4 (36.4%)
 4        H2        Female      7       0.6363    7 (63.6%)

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

So if you have actual percentages (so 0.2222 instead of 22.22 )

data CP; 
  input Treatment $ SEX $ COUNT PERCENT :percent.;
cards;
H1 Male 2 22.22%
H1 Female 7 77.77%
H2 Male 4 36.36%
H2 Female 7 63.63%
;

You could use the fact that the PERCENT format adds () around negative values to make it easier.

data want;
  set cp;
  length string $20;
  string=put(count,comma12.)||' '|| put(-percent,percent7.1);
run;
Obs    Treatment     SEX      COUNT    PERCENT     string

 1        H1        Male        2       0.2222    2 (22.2%)
 2        H1        Female      7       0.7777    7 (77.8%)
 3        H2        Male        4       0.3636    4 (36.4%)
 4        H2        Female      7       0.6363    7 (63.6%)

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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