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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 801 views
  • 1 like
  • 4 in conversation