BookmarkSubscribeRSS Feed
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 = .; 
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 = .; 

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});

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


Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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