BookmarkSubscribeRSS Feed
hema_bhure
Calcite | Level 5

Hi ,

Thanks in advance i need help in the following query.

The data i have as follows

The input dataset

-----------------------------------

acc  subac amt 

111    1       -120.83

111    1       -120.83

111     2       -82.04

111     2        10.04

212    6        34.89

212     6        10.02

123    1       785.90

123     1      785.90

123     1      `1900.09

 

so I need data need to compare the current amt with previous amt on basis of account and subac and if amt is diff then raise a flag=1

The following dataset i want.

------------------------------------------

acc  subac    amt      flag

111    1       -120.83    0

111    1       -120.83    0

111     2       -82.04     0

111     2        10.04      1

212    6        34.89     0

212     6        10.02    1

123    1       785.90    0

123     1      785.90    0

123     1      1900.09   1

 

please help me out. 

2 REPLIES 2
Tom
Super User Tom
Super User

The DIF() function will compare the current value with the value passed to it before.  So you must execute it on every observation to have it compare to the value on the previous observation.

 

Do you want the flag to be zero (FALSE) for the first observation in a group?

 

Your data is not actually sorted, but it does appear to be grouped, so use the NOTSORTED keyword on the BY statement.

data want;
 set have;
 by acc subac notsorted;
 flag = dif(amt) and not first.subac;
run;
ballardw
Super User

In your want example you show

The following dataset i want.
------------------------------------------
acc  subac    amt      flag
111    1       -120.83    0
111    1       -120.83    0
111     2       -82.04     0
111     2        10.04      1

So why is -82.04 NOT considered different than -120.83 ?????

 


@hema_bhure wrote:

Hi ,

Thanks in advance i need help in the following query.

The data i have as follows

The input dataset

-----------------------------------

acc  subac amt 

111    1       -120.83

111    1       -120.83

111     2       -82.04

111     2        10.04

212    6        34.89

212     6        10.02

123    1       785.90

123     1      785.90

123     1      `1900.09

 

so I need data need to compare the current amt with previous amt on basis of account and subac and if amt is diff then raise a flag=1

The following dataset i want.

------------------------------------------

acc  subac    amt      flag

111    1       -120.83    0

111    1       -120.83    0

111     2       -82.04     0

111     2        10.04      1

212    6        34.89     0

212     6        10.02    1

123    1       785.90    0

123     1      785.90    0

123     1      1900.09   1

 

please help me out. 


 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2112 views
  • 0 likes
  • 3 in conversation