Hi, I am having problems creating a new Column where I have to use a condition.
The context is: I have a table with the columns:
-ID
-credit card: it can be visa or master.
-amount: the amount in $
The objetive is not to be repetitive with the id if the client has visa and master. So I need 2 new columns where: if the credit card is from visa the new column "Amount_Visa" has the consolidated amount. And if its from master, the new column "Amount_Master" has its amount.
Now I have this:
ID | Credit card | Amount |
1 | visa | 230.60 |
1 | master | 456.30 |
And I need this:
ID | Credit Card | Amount_Visa | Amount_Master |
1 | Visa/Master | 230.60 | 456.30 |
Thank you!
data have;
input ID CreditCard $ Amount;
datalines;
1 visa 230.60
1 master 456.30
2 visa 230.60
2 master 456.30
;
data want;
do until (last.ID);
set have;
by ID;
CreditCard = "V/M";
Amount_Visa = Amount;
Amount_Master = Amount;
end;
drop Amount;
run;
data have;
input ID Credit_card $ Amount;
cards;
1 visa 230.60
1 master 456.30
;
proc transpose data=have out=want(drop=_name_) prefix=Amount_;
by id;
var amount;
id credit_card;
run;
proc print noobs;run;
data have;
input ID CreditCard $ Amount;
datalines;
1 visa 230.60
1 master 456.30
2 visa 230.60
2 master 456.30
;
data want;
do until (last.ID);
set have;
by ID;
CreditCard = "V/M";
Amount_Visa = Amount;
Amount_Master = Amount;
end;
drop Amount;
run;
Both approaches handles 3000 obs easily.
@clipsia wrote:
Thank you both! But does it help if I have 3000 rows in this table??
With a sufficiently slow computer, it might actually need more than 0.01 seconds.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.