DATA Step, Macro, Functions and more

Adding Difference to Individual Observation of a data set

Reply
New Contributor
Posts: 3

Adding Difference to Individual Observation of a data set

DATA Deposits;

input Acctno type $ Balance;

cards;

1 Demand 10

2 Time 20

3 Demand 10

4 Saving 30

5 Demand 10

6 Saving 30

7 Time 20

8 Saving 30

9 Demand 10

;

Run;

%let Diff_DD = 1;

%let Diff_SD = 2;

%let Diff_TD = 3;

I need o add      Diff_DD value to the "1 Demand 10"

                        Diff_TD value to the "2  Time 20"

                        Diff_SD value to the "4 Saving 30"

Please help

Super Contributor
Posts: 259

Re: Adding Difference to Individual Observation of a data set

"Add" to what? Do you need a new variable? Please post the dataset you want to create.

New Contributor
Posts: 3

Re: Adding Difference to Individual Observation of a data set

Do you want to concatenate the macro variable value to concatenation of other 3 variables ?

Contributor
Posts: 52

Re: Adding Difference to Individual Observation of a data set

Declare the macro variables first, then add the difference variable in the same DATA step:

%let Diff_DD = 1;

%let Diff_SD = 2;

%let Diff_TD = 3;

data deposits;

   input acctno type $ balance;

  

   select (acctno);

      when (1) difference = &Diff_DD;

      when (2) difference = &Diff_SD;

      when (4) difference = &Diff_TD;

      otherwise;

   end;

   cards;

               {etc}

;

run;

New Contributor
Posts: 3

Re: Adding Difference to Individual Observation of a data set

Guys Thank you for you reviews in this issue, I was in vacation so sorry for late reply.

1 Demand 11

2 Time 20

3 Demand 10

4 Saving 32

5 Demand 10

6 Saving 30

7 Time 23

8 Saving 30

9 Demand 10

;

Run;

@

PROC Star
Posts: 7,360

Re: Adding Difference to Individual Observation of a data set

Why are the values only added some of the time?  We can see that you want to sum with the three additional values for demand, time and saving, but you haven't given any clue as to why you are only doing it with certain records.

Super User
Posts: 5,080

Re: Adding Difference to Individual Observation of a data set

If you want to apply these changes on every observation, you can do it with one additional statement:

balance = balance + &DIFF_DD * (type='Demand') + &DIFF_SD * (type='Saving') + &DIFF_TD * (type='Time');

As others have noted, your intention isn't 100% clear, even based on your revised posting.  Why is &DIFF_TD added to "7 TIme 23" but not to "2 Time 20" ?  Based on your answer, there will likely be further coding necessary.  It won't be difficult, but it will require a clear understanding of what you are trying to achieve.

Good luck.

Contributor
Posts: 45

Re: Adding Difference to Individual Observation of a data set

OniMoni, indeed answering Art Tabachneck's question would clear things up.

In the meantime, here is an ever shorter form of Astounding's solution, even making the macro variable unnecessary:

balance = balance + (whichc(type, 'Demand', 'Saving', 'Time'));

Cheers.

Ask a Question
Discussion stats
  • 7 replies
  • 442 views
  • 0 likes
  • 7 in conversation