BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

In my data set the following variable present.

 if there is any change (variable CHG) by id and avisitn, greater than 10, then i need to populate (chgcat1='Max increase >10') for all obs having same id and avisitn

 

id                    avisitn                   chg                 test
1                       2                          8                     sbp
1                        2                          9                    sbp
1                       2                          7                     sbp

1                        3                          12                   dbp

1                       3                           4                      dbp
2                      1                           10                   sbp
2                      1                            11                   sbp
2                     1                              5                     sbp
2                      1                             4                     sbp



output needed


id                          avisitn                      chg                                chgcat1
1                            2                               8
1                            2                               9
1                            2                               7

1                            3                               12                             Max increase >10

1                            3                                 4                             Max increase >10
2                           1                               10                            Max increase >10
2                           1                               11                              Max increase >10
2                           1                                5                                Max increase >10
2                           1                                4                                 Max increase >10

 

The chgcat1 is blank for first 3 obs because none of the obs with id=1 and avisitn=2 have chg greater than 10.

 

I think i can do this in two data steps. is there any way I can in one step as i have to calculate 12 chgcat. Thanks

3 REPLIES 3
mkeintz
PROC Star

Read each group twice.

  In the first group read look for chg>10

 

  In the second group "re-read" output the records:

 

data want;

  do until (last.avistn);

    set have;

    by id avistn;

    if chg>10 then chgcat='Max Increase > 10';

  end;

 

  do until (last.avistn);

    set have;

    by id avistn;

    output;

  end;

run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Dan_B
Fluorite | Level 6

I'd use the retain statement and have the data sorted. Then you can process in a single datastep as follows:

proc sort data=work.data1;
  by id avisitn descending chg;
run;

data work.data2;
  set work.data1;
  by id avisitn descending chg;
  retain chgcat;
  length chgcat $20;
  if first.avisitn then chgcat = '';
  if chg>10 then chgcat='Max Increase > 10';
run;
Ksharp
Super User
You mean MAX > 10 to MAX-MIN > 10 ?


data have;
input id                    avisitn                   chg ;
cards;
1                       2                          8                     sbp
1                        2                          9                    sbp
1                       2                          7                     sbp
1                        3                          22                   dbp
1                       3                           4                      dbp
2                      1                           10                   sbp
2                      1                            11                   sbp
2                     1                              5                     sbp
2                      1                             4                     sbp
;
run;
proc sql;
select *,case when range(chg) gt 10 then 'Max increase >10' else ' ' end as f length=20
 from have
  group by id,avisitn;
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
  • 3 replies
  • 1430 views
  • 3 likes
  • 4 in conversation