How can i identify in a data set a vairable with value and when found to let 3 follwing variable to be zero
Input data
Accts | Data_Q1_2017 | Data_Q2_2017 | Data_Q3_2017 | Data_Q4_2017 | Data_Q1_2018 |
Acc1 | 1 | 2 | 3 | ||
Acc2 | 1 | 2 | 3 | ||
Acc3 | 3 | ||||
Acc4 | 1 | 1 | 3 | ||
Acc5 | 1 | 1 | 3 |
Requested output
Accts | Data_Q1_2017 | Data_Q2_2017 | Data_Q3_2017 | Data_Q4_2017 | Data_Q1_2018 |
Acc1 | 1 | 0 | 0 | 0 | 3 |
Acc2 | 1 | 0 | 0 | ||
Acc3 | 3 | ||||
Acc4 | 1 | 0 | 0 | 0 | |
Acc5 | 1 | 0 | 0 | 0 | 3 |
UNTESTED CODE
data want;
set have;
array d data_q1_2017--data_q1_2018;
do i=1 to dim(d);
if d(i)=2 then do;
if i+1 <= dim(d) then d(i+1)=0;
if i+2 <= dim(d) then d(i+2)=0;
if i+3 <= dim(d) then d(i+3)=0;
end;
end;
run;
If you want tested code, do not supply data as a screen capture, instead supply data as SAS data step code via these instructions: https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/
UNTESTED CODE
data want;
set have;
array d data_q1_2017--data_q1_2018;
do i=1 to dim(d);
if d(i)=2 then do;
if i+1 <= dim(d) then d(i+1)=0;
if i+2 <= dim(d) then d(i+2)=0;
if i+3 <= dim(d) then d(i+3)=0;
end;
end;
run;
If you want tested code, do not supply data as a screen capture, instead supply data as SAS data step code via these instructions: https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/
It works
Thanks a lot 🙂
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.