user2827927
user2827927

Reputation: 3

Need help for a SAS problem to get a new variable created basis on the conditional data

I have a data as follows - Credit card transaction data

I need to add a counter variable for each credit card -- for each date and transaction country type. which should be look like this -Expected result with New counter variable

I am trying this with the help of by group (first. last.) but not getting expected result. Help needed for this problem statement. Thanks!!

Upvotes: 0

Views: 38

Answers (1)

Greg
Greg

Reputation: 138

You are on the right track with applying the concepts first. and last.. Let me know if this works.

proc sort data=work.trans_data;
    by cc_no dot country ts;
run;

data work.trans_data_cnt;
    set work.trans_data;
    by cc_no dot country ts;

    /*This will assign the first row of the cc_no, dot, country = 1*/
    if first.cc_no = 1 and first.dot = 1 and first.country = 1 then
        counter = 1;
    /*Restart counter, we have switched country, but are in the same cc_no and dot.*/
    else if first.country = 1 then
        counter = 1;
    /*Increment the counter, we are within the same cc_no, dot, country.*/
    else
        counter + 1;
run;

Upvotes: 0

Related Questions