SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

Hello,

 

how do I add a ")" at the end of the variable if it's missing a ")"?

 

data:

var

(100.00%

(99.0%)

(10.00%)

(5.55%)

(99%

 

want:

(100.00%)

(99.0%)

(10.00%)

(5.55%)

(99%)

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@HitmonTran Based on below sample code it appears that informat PERCENT can deal with such source data.

If the parenthesis mean minus percent values then I'd keep the numerical variable num_var for further processing. 

data sample;
  input string $12.;
  format num_var percent9.2;
  num_var=input(string,percent9.);
  str_var=compress(put(num_var,percent9.2));
  datalines;
(100.00%
(99.0%)
(10.00%)
(5.55%)
(99%
25.56%
;

 Patrick_0-1667010542325.png

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

If these are text strings, this ought to work

 

data have;
    input string $12.;
    cards;
(100.00%
(99.0%)
(10.00%)
(5.55%)
(99%
;

data want;
    set have;
    if left(reverse(string)) ^=: ')' then string=cats(string,')');
run;

 

Please note the colon modifier in the IF statement, this is important.

 

Also important, if you data is arriving sometimes with right parentheses and sometimes without right parentheses, then your process needs to be fixed. In the long run, that is a much better solution than a band-aid like this.

 

With regards to providing us with sample data, @HitmonTran please note and follow my lead in the future, the data is provided via working SAS data step code, and not just by showing us a list.

--
Paige Miller
ballardw
Super User

Is the value supposed to represent a negative percentage?

 

Is the variable numeric or character? Proc Contents will help with this if you aren't exactly sure.

If the existing variable has a length of 8  , implied from your value (100.00%, and is character then you will need to create a new variable as there is not enough enough space to insert the character.

How exactly is the value created? That would be the place to address this instead of "fixing" later. Show the code.

 

Best is to provide example data in the form of a data step so we know exactly what we are dealing with.

 

 

Patrick
Opal | Level 21

@HitmonTran Based on below sample code it appears that informat PERCENT can deal with such source data.

If the parenthesis mean minus percent values then I'd keep the numerical variable num_var for further processing. 

data sample;
  input string $12.;
  format num_var percent9.2;
  num_var=input(string,percent9.);
  str_var=compress(put(num_var,percent9.2));
  datalines;
(100.00%
(99.0%)
(10.00%)
(5.55%)
(99%
25.56%
;

 Patrick_0-1667010542325.png

 

PaigeMiller
Diamond | Level 26

@Patrick says:

If the parenthesis mean minus percent values then I'd keep the numerical variable num_var for further processing. 

 

I agree, that's a much better solution than handling percents as character strings. And so, I'd like to withdraw my solution above.

--
Paige Miller
PaigeMiller
Diamond | Level 26

@HitmonTran 

Thank you for marking my solution correct, but since then I have withdrawn my solution in favor of the solution from @Patrick which in my opinion is clearly a better solution. Would you be kind enough to mark Patrick's answer as correct?

--
Paige Miller
Ksharp
Super User
data have;
    input string $12.;
    cards;
(100.00%
(99.0%)
(10.00%)
(5.55%)
(99%
;

data want;
    set have;
    if not findc(string,')') then string=cats(string,')');
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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