BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
esita
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
array Expense_[4]  car_expense book_expense exp_office exp_gas;

View solution in original post

8 REPLIES 8
data_null__
Jade | Level 19

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.

esita
Calcite | Level 5

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
data_null__
Jade | Level 19

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
    esita
    Calcite | Level 5

    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.

    data_null__
    Jade | Level 19
    array Expense_[4]  car_expense book_expense exp_office exp_gas;
    esita
    Calcite | Level 5

    Thank You!

    Reeza
    Super User

    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.

    esita
    Calcite | Level 5

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

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

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