admin管理员组

文章数量:1391987

suppose to have the following:

data DB;
  input year week path :$20. Value ;
cards;
2014 40 Covid     30
2014 40 Covid     20
2014 40 Covid     10
2014 40 Influenza 4
2014 41 Covid     15
2014 41 Covid     10
2014 41 Covid     3
2014 41 Covid     2
2014 41 Influenza 50
;

Is there a way to get the following?

data DB1;
  input year week path :$20. Value Flag;
cards;
2014 40 Covid     30     1
2014 40 Covid     20     0
2014 40 Covid     10     0
2014 40 Influenza 4      1
2014 41 Covid     15     1
2014 41 Covid     10     0
2014 41 Covid     3      0
2014 41 Covid     2      0
2014 41 Influenza 50     1
;

In other words, add a Flag variable as follows: for repeated "path" Flag variable = 1 to the first (data are sorted by year, week, path and descending value). If the path is not repeated (like Influenza) then Flag = 1. So, basically the rule applies only to repeated values of path where the first = 1 while the remaining = 0.

Thank you in advance

suppose to have the following:

data DB;
  input year week path :$20. Value ;
cards;
2014 40 Covid     30
2014 40 Covid     20
2014 40 Covid     10
2014 40 Influenza 4
2014 41 Covid     15
2014 41 Covid     10
2014 41 Covid     3
2014 41 Covid     2
2014 41 Influenza 50
;

Is there a way to get the following?

data DB1;
  input year week path :$20. Value Flag;
cards;
2014 40 Covid     30     1
2014 40 Covid     20     0
2014 40 Covid     10     0
2014 40 Influenza 4      1
2014 41 Covid     15     1
2014 41 Covid     10     0
2014 41 Covid     3      0
2014 41 Covid     2      0
2014 41 Influenza 50     1
;

In other words, add a Flag variable as follows: for repeated "path" Flag variable = 1 to the first (data are sorted by year, week, path and descending value). If the path is not repeated (like Influenza) then Flag = 1. So, basically the rule applies only to repeated values of path where the first = 1 while the remaining = 0.

Thank you in advance

Share Improve this question asked Mar 12 at 10:26 NewUsr_statNewUsr_stat 2,5835 gold badges29 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Isn't this as simple as:

data want;
   set DB;
   by year week path;
   Flag = first.path;
run;

Result:

year  week  path       Value  Flag
2014  40    Covid      30     1
2014  40    Covid      20     0
2014  40    Covid      10     0
2014  40    Influenza  4      1
2014  41    Covid      15     1
2014  41    Covid      10     0
2014  41    Covid      3      0
2014  41    Covid      2      0
2014  41    Influenza  50     1

本文标签: sasAssign a value to a new variable based on the first occurrence of another variable valueStack Overflow