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%)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 841 views
  • 0 likes
  • 2 in conversation