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

Hi there,

I need to group my data based on condition of (current year -10 and below) in one group and remaining all in other group in my dataset. What should be possible chances apart from proc format.

Data new.

input year price1 price2 price3

1999 100 200 300

2000 200 300 400

1996 500 400 800

2002 7000 9000 1200

1992 6000 2000 10000

run,

So I want data like this

Year price1 price2 price3

1992 6000 2000 10000

1996 500 400 800

1999 100 200 300

2000 200 300 400

2002 7000 9000 1200

So what should be my best bet .. here we have current year set as 2011.... I do not want to display groups as something else... I want it to be printed out as it is... but just reordered... Any suggestion is highly appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I agree with Peter regarding including a proc sort, but you may also want to use if then logic or, similarly, the ifn function.  E.g.,

data have;

  input year price1 price2 price3;

  cards;

1999 100 200 300

2000 200 300 400

1996 500 400 800

2002 7000 9000 1200

1992 6000 2000 10000

;

proc sort data=have;

  by year;

run;

data want;

  set have;

  group=ifn(year le year(today())-10,1,2);

run;

proc print data=want;

  by group;

run;

View solution in original post

5 REPLIES 5
Peter_C
Rhodochrosite | Level 12

proc sort?

art297
Opal | Level 21

I agree with Peter regarding including a proc sort, but you may also want to use if then logic or, similarly, the ifn function.  E.g.,

data have;

  input year price1 price2 price3;

  cards;

1999 100 200 300

2000 200 300 400

1996 500 400 800

2002 7000 9000 1200

1992 6000 2000 10000

;

proc sort data=have;

  by year;

run;

data want;

  set have;

  group=ifn(year le year(today())-10,1,2);

run;

proc print data=want;

  by group;

run;

yash82
Calcite | Level 5

Wow... thanks guys...I never thought about ifn function though...

MikeZdeb
Rhodochrosite | Level 12

hi ... you could also use Art's suggestion in one step ...

proc sql;

create table want as

select *, (year gt year(today())-10) as group from have

order by group, year;

quit;

art297
Opal | Level 21

Actually, since you are only trying to identify two groups, I like Mike's suggestion of calculating a boolean better than using ifn.  And, that can be done in either a datastep or proc sql.

However, if you decide to use proc sql, I'd remove group from the order by clause, as it is already accounted for by year.  Interestingly, such a small change, improves performance by almost 50%:

proc sql;

  create table want as

    select *,

      year gt year(today())-10 as group

         from have

           order by year

  ;

quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 1136 views
  • 1 like
  • 4 in conversation