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%)
@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%
;
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.
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.
@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 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.
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?
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;
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.
Ready to level-up your skills? Choose your own adventure.