BookmarkSubscribeRSS Feed
duckieduck
Calcite | Level 5

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!

1 REPLY 1
mkeintz
PROC Star

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).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 453 views
  • 1 like
  • 2 in conversation