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 🙂
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.