Hi! I'm repeating the same process for 13 prefixed and am wondering if it could be done with array methods. Here is the code I tried but got an error message:
data data_wide;
set data_array;
array prefix{13} pa pb pc pd pe pf pg ph pi pj pk pl pm;
do n = 1 to 13;
if prefix{n}_15 = . or prefix{n}_45 = . then do;
prefix{n}_15 = .;
prefix{n}_45 = .;
end;
end;
run;
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.
I don't want to revert back to just copy-and-pasting the same code 13 times but how could I make the process more efficient and neat? Would appreciate any help. Thank you!
I believe you are effectively trying to do this:
if pa_15 = . or pa_45 = . then do;
pa_15 = .;
pa_45 = .;
end;
followed by the same for pb_15,pb_45 through pm_15,pm_45. Two suggestions :
First, don't make a do group. Instead use code like this:
if n(pa_15,pa_45)=1 then call missing(pa_15,pa_45);
followed by pb through pm.
Second, to use arrays, use this structure:
array p15 {13} pa_15 pb_15 pc_15 pd_15 pe_15 pf_15 pg_15
ph_15 pi_15 pj_15 pk_15 pl_15 pm_15;
array p45 {13} pa_45 pb_45 pc_45 pd_45 pe_45 pf_45 pg_45
ph_45 pi_45 pj_45 pk_45 pl_45 pm_45;
do i=1 to 13;
if n(p15{i},p45{i})=1 then call missing(p15{i},p45{i});
end;
To do it the way you originally conceived, you would have to use sas macro coding, which does not seem to be well-justified, given the relatively simple technique I'm suggesting.
Edit note: the do loop above was corrected (6/19/2024, 14:00 UT).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.