DATA Step, Macro, Functions and more

Proc Append in Macro

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Proc Append in Macro

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.


Accepted Solutions
Solution
‎02-03-2015 05:43 PM
Respected Advisor
Posts: 3,799

Re: Proc Append in Macro

array Expense_[4]  car_expense book_expense exp_office exp_gas;

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Proc Append in Macro

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.

Contributor
Posts: 25

Re: Proc Append in Macro

Posted in reply to data_null__

This is my Input data

Idexpense_1expense_2expense_3expense_4
1-50025058-87
24003607869
3-360-54-8554
4-80-6923-77
550095-3032

And this is what I want

Idexpense_1expense_2expense_3expense_4crd_1crd_2crd_3crd_4
1-50025058-87-111-1
240036078691111
3-360-54-8554-1-1-11
4-80-6923-77-1-11-1
550095-303211-11
Respected Advisor
Posts: 3,799

Re: Proc Append in Macro

SIGN function and ARRAY.

data exp;
   length id $4;
  
array Expense_[4];
   array crd_[4];
   input Id Expense_
  • ;
  •    do i = 1 to dim(Expense_);
          crd_ = sign(Expense_);
         
    end;
      
    drop i;
       cards;
    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
    ;;;;
       run;
    proc print;
      
    run;

    2-3-2015 3-41-33 PM.png
    Contributor
    Posts: 25

    Re: Proc Append in Macro

    Posted in reply to data_null__

    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.

    Solution
    ‎02-03-2015 05:43 PM
    Respected Advisor
    Posts: 3,799

    Re: Proc Append in Macro

    array Expense_[4]  car_expense book_expense exp_office exp_gas;
    Contributor
    Posts: 25

    Re: Proc Append in Macro

    Posted in reply to data_null__

    Thank You!

    Super User
    Posts: 19,822

    Re: Proc Append in Macro

    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.

    Contributor
    Posts: 25

    Re: Proc Append in Macro

    Reeza, that is a typo. I don;t have "is" in my code.

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 8 replies
    • 288 views
    • 3 likes
    • 3 in conversation