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.
subrat1
Fluorite | Level 6

I have two column :

 

UNITAmount
$-162
$-398
$160

 

When i concatenate My amount is coming as $-160 , but i want it to come as -$160 .

however if it positive no then it should come like $160

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Since the problem involves changing a string into another string, you could use regular expression substitution:

 

data test;
input amount $;
dollarAmount = prxchange("s/([.0-9])/\$\1/",1,amount);
datalines;
-162
-398
160
;

proc print noobs; run;
PG

View solution in original post

5 REPLIES 5
kiranv_
Rhodochrosite | Level 12

somthing like this should work


data have;
input UNIT $ Amount;
datalines;
$ -162 
$ -398 
$ 160
;



proc sql;
select case when amount lt 0
 then '-'||strip(unit)||strip(put(abs(amount),5.))
 else strip(unit)||strip(put(amount,5.))
 end as newcol
 from have;
PGStats
Opal | Level 21

Since the problem involves changing a string into another string, you could use regular expression substitution:

 

data test;
input amount $;
dollarAmount = prxchange("s/([.0-9])/\$\1/",1,amount);
datalines;
-162
-398
160
;

proc print noobs; run;
PG
ChrisHemedinger
Community Manager

And if you need something more robust to apply a real currency format, you could test for different currency "unit" values and apply the proper formats according to the regional conventions.

 

data test;
 length unit $ 3 amount 8;
 infile datalines;
 input unit amount;
 datalines;
$ -162
$ -398
$ 160
GBP -162
GBP -398
GBP 160
€ -162
€ -398
€ 160
;
run;

data formatted;
 set test;
 select (unit);
  when ('$')  price = put(amount,dollar10.);
  when ('GBP')  price = put(amount,NLMNLGBP10.);
  when ('€')  price = put(amount,NLMNLEUR10.); /* or EURO10. */
  otherwise price=put(amount,dollar10.);
 end;
run;

 

fmtcurrency.png

 

(I don't know why the euro symbol isn't showing in records 7 and 8...but you get the idea, I hope.)

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
PGStats
Opal | Level 21

Strangely enough, I don't get the same result with my version of SAS (and/or locale)

 

                       Obs    unit    amount      price

                         1     $        -162          $-162
                         2     $        -398          $-398
                         3     $         160           $160
                         4     GBP      -162     (162,00 £)
                         5     GBP      -398     (398,00 £)
                         6     GBP       160      160,00 £
                         7     €        -162     (162,00 €)
                         8     €        -398     (398,00 €)
                         9     €         160      160,00 €
PG
ChrisHemedinger
Community Manager

@PGStats  - the NL* formats are meant to be locale-sensitive, so if you're running in French Canadian, that might be different than my English-US.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!

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
  • 5 replies
  • 4025 views
  • 2 likes
  • 4 in conversation