Hi All,
I'm trying to assign a value to 40 variables in a data set using if then statement in MACRO. But When I execute it, it assigns value to only last variable in the macro list. Here is my code.
%macro up (var, var2);
data abc_new;
set abc;
if &var is <0 then &var2= -1;
else if &var is >0 then &var2 = 1;
run;
%mend up;
%up (expense_1, crd_1);
%up (expense_2, crd_2);
"
"
"
%up (expense_40, crd_40);
when I execute this code my data set only contains crd_40. I don't see crd_1 thru crd_39.
From what I can see you are overwriting ABC_NEW each time. You would need the MODIFY statement to APPEND as written.
However perhaps you can do it ALL in one simple data set. Show example input data and example of what you want.
This is my Input data
Id | expense_1 | expense_2 | expense_3 | expense_4 |
1 | -500 | 250 | 58 | -87 |
2 | 400 | 360 | 78 | 69 |
3 | -360 | -54 | -85 | 54 |
4 | -80 | -69 | 23 | -77 |
5 | 500 | 95 | -30 | 32 |
And this is what I want
Id | expense_1 | expense_2 | expense_3 | expense_4 | crd_1 | crd_2 | crd_3 | crd_4 |
1 | -500 | 250 | 58 | -87 | -1 | 1 | 1 | -1 |
2 | 400 | 360 | 78 | 69 | 1 | 1 | 1 | 1 |
3 | -360 | -54 | -85 | 54 | -1 | -1 | -1 | 1 |
4 | -80 | -69 | 23 | -77 | -1 | -1 | 1 | -1 |
5 | 500 | 95 | -30 | 32 | 1 | 1 | -1 | 1 |
SIGN function and ARRAY.
Thank You for the piece of code. This is helpful, but the variables Expense1, expense_2.... are actually named different. Like car_expense, book_expense, exp_office, exp_gas and so on. I named it as Expense_1...expense_40 just for example code.
Thank You!
Your IF statements are incorrect assuming that is your actual code, you have an IS word that doesn't belong there.
if &var <0 then &var2= -1;
else if &var>0 then &var2 = 1;
This also seems perfect for arrays rather than macros.
Reeza, that is a typo. I don;t have "is" in my code.
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.