Hello,
I have a character variable named "Rate" that I am importing from excel that is $38. It represents a monetary value and appears has either $XXX.XX or ($XXX.XX) --The parenthesis represent negative amounts. I am trying to work with this variable in proc sql to convert it from character to numeric, then sum it up and add total to the table so I can then export it again in excel using ods tagsets and proc print. This is all occurring in a macro as well.
Here is a snippet of my code:
proc sql;
create table mergembr as
select a.participantID, a.servicesite, a.dos, a.procedure, a.MBRlabel,
a.paymentsource, input(a.rate,38) format comma12.2, b.servicesite, b.sfs_provider_name, b.hri_and_or_SFS_address, b.sfs_address
from work.stateNew_MBR_51 as a
left join work.credfilesfs as b
on input(a.servicesite,best8.)=b.servicesite;
insert into mergembr
select sum(a.rate)
from work.stateNew_MBR_51;
quit;
Wrong informat:
data example; string= '($1,456,899,234.56)'; num = input(string,comma32.); run;
Note that the limit on the comma format is 32. If there are actually 38 characters in a currency value you will need to do something. Without examples it is hard to tell what that something might actually be.
I am having some luck with this but it is removing the parenthesis when I think I'll ultimately need them to be negatives so that it sums up correctly...
proc sql;
create table mergembrnew as
select servicesite, dos, rate, input(compress(rate,'0123456789','k'),dollar32.2) as newrate
from work.stateNew_MBR_51;
quit;
run;
ahhh yes...It is converting like I need it to! Now I am going to try to sum it and insert the sum as a total in the output
advice on how to sum and insert the totals into the bottom of the table?
proc sql;
create table mergembr as
select a.participantID, a.clientname, a.DOB, a.servicesite, a.dos, a.procedure, a.MBRlabel,
a.paymentsource,a.rate,input(a.rate,comma32.) as newrate, b.servicesite, b.sfs_provider_name, b.hri_and_or_SFS_address, b.sfs_address
from work.stateNew_MBR_51 as a
left join work.credfilesfs as b
on input(a.servicesite,best8.)=b.servicesite;
insert into mergembr
select sum(calculated newrate)
from work.stateNew_MBR_51;
quit;
Try nlmny10. or rstdodn10. format if you want to see parenthesis for negative currency values.
If you want totals at the bottom of the table then PROC SQL might not be the right approach. You may need to try PROC REPORT or PROC TABULATE. Show provide some sample data in the form of a datastep please.
great, the proc print sum is working perfectly thank you. Didn't realize I could use that! 🙂
If your using SAS 9.4 M3 or later, then try ODS EXCEL instead of ODS EXCELXP.TAGSETS to create native Excel files (xlsx). Only thing you need to worry about ODS EXCEL is insufficient memory issue when your trying to send large table into Excel. ODS Tagsets inserts one row at a time, where as ODS EXCEL will insert the whole table at once which cause insufficient memory issues.
Consider this SAS Paper if you prefer ODS Excel
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!
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.