Hi....I am trying to correct the entries in Dept for the same ID by changing them to the entry that is entered for Dept when Section = s1 except for when Dept=a4. Any suggestions? Thanks
data one;
input ID Dept$ Section$ credit;
datalines;
1 a1 s1
1 a1 s2 0.5
1 a2 s3 1.0
1 a2 s4 1.0
1 a4 s5 1.0
1 a4 s6
2 a1 s3 1.0
2 a1 s2 0.5
2 a2 s1
2 a3 s4 1.0
2 a4 s5 1.0
2 a1 s5 0.5
2 a1 s5 1.0
2 a1 s6 1.0
2 a3 s6
2 a2 s6 1.0
3 a4 s5 0.5
3 a2 s5
3 a2 s5 1.0
3 a1 s1
3 a1 s2 1.0
3 a1 s3 1.0
;
run;
Want:
data want;
1 a1 s1
1 a1 s2 0.5
1 a1 s3 1.0
1 a1 s4 1.0
1 a4 s5 1.0
1 a4 s6
2 a2 s3 1.0
2 a2 s2 0.5
2 a2 s1
2 a2 s4 1.0
2 a4 s5 1.0
2 a2 s5 0.5
2 a2 s5 1.0
2 a2 s6 1.0
2 a2 s6
2 a2 s6 1.0
3 a4 s5 0.5
3 a1 s5
3 a1 s5 1.0
3 a1 s1
3 a1 s2 1.0
3 a1 s3 1.0
One way:
data WANT;
merge ONE ONE(keep=ID DEPT SECTION rename=(DEPT=D SECTION=S) where=(S='s1'));
by ID;
if DEPT ne 'a4' then DEPT=D;
drop D S;
run;
[Edited to add a4 exception]
One way:
data WANT;
merge ONE ONE(keep=ID DEPT SECTION rename=(DEPT=D SECTION=S) where=(S='s1'));
by ID;
if DEPT ne 'a4' then DEPT=D;
drop D S;
run;
[Edited to add a4 exception]
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.