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 badges1 Answer
Reset to default 0Isn'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
版权声明:本文标题:sas - Assign a value to a new variable based on the first occurrence of another variable value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744758896a2623631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论