DATA Step, Macro, Functions and more

Array not working

Reply
Regular Contributor
Posts: 194

Array not working

[ Edited ]

Hi, I have the following code but it isn't working as expected:

data want;
	set have;
    array pct  all_pct con_pct me_pct be_pct  oth_pct;

	array vacpct d_pct p_pct m_pct h_pct hp_pct v_pct;

	array origvacpct pct_d pct_p pct_m pct_h pct_hb pct_v; 

	array vacnum dvac pvac mvac hvac hpvac vvac;

  do i=1 to dim (pct);
	 if pct(i)='90+' and origvacpct(i) le 10 then do;
		vacpct(i)='<=10'; 
		vacnum(i)='--';	
	 end;
     if pct(i)='95+' and origvacpct(i) le 5 then do;
		vacpct(i)='<=5';
	        vacnum(i)='--';
	 end;
     if pct(i)='99+' and origvacpct(i) le 1  then do;
		vacpct(i)='<=1';
		vacnum(i)='--';
	 end;

 end;
run;

The variables in the array origvacpct are numeric.

 

The rest of the variables in the remaining arrays are character.

 

For variables in the pct array that are  '90+' we need the values in the array vacpct to be  '<=10' if the variables values in the array origvacpct are <=10.  And we need the corresponding variables in the array vacnum to be '--'.

 

For variables in the pct array that are  '95+' we need the values in the array vacpct to be  '<=5' if the variables values in the array origvacpct are <=5.  And we need the corresponding variables in the array vacnum to be '--'.

 

For variables in the pct array that are  '99+' we need the values in the array vacpct to be  '<=1' if the variables values in the array origvacpct are <=1.  And we need the corresponding variables in the array vacnum to be '--'.

 

 

However, the output doesn't consistently code the variables in vacpct and vacnum as expected.  I get no error messages.

What am I doing wrong?  Thank so much!

PROC Star
Posts: 7,471

Re: Array not woking

It would help to have some test data, but here are two initial thoughts:

1. You use sevenpct(i) in your code, but I didn't see such an array being declared. However, that should have resulted in an error

2. You don't include a $ before the variable list in your character arrays

 

Also, I typically declare the array size (e.g. pct(*) or pct(4), or the like, but I don't think that is your problem if the code isn't producing an error.

 

Art, CEO, AnalystFinder.com

Regular Contributor
Posts: 194

Re: Array not woking

[ Edited ]

Thanks for your feedback. Question: if all the variables exist already, do I still need a dollar sign before the variables in my array? 
I don't have them in other areas of my code and it still runs with the expected output.  Is the dollar sign only if you are creating new variables? 

Also it is is a good practice to declare the variable length and whether it is character or numeric even if the variables already exist? If so, can you help me understand why? 

 

PROC Star
Posts: 7,471

Re: Array not woking

[ Edited ]

If the variables already exist then, no, you don't have to declare the arrays as being character or their length.

 

As for your other question, I don't think there is an officicial SOP. Whatever works best for you and allows your code to be both understood by others and most easily maintainable. However, in that regard, your code only uses implicit arrays (i.e., arrays without subscripts). Those aren't even mentioned in the documentation anymore, so I wouldn't recommend using them.

 

One question though. Your pct array only has five variables, while your others each have six. Was that a typo?

 

Back to your original question, your providing some example have and want data will make it easier for anyone to let you know if your code appears to be working as intended.

 

Art, CEO, AnalystFinder.com

New Contributor
Posts: 3

Re: Array not working

Could you show us the input data you used, and output you got as well? That would be helpful to solve your issue.

Super User
Posts: 11,343

Re: Array not working

 

Hi, I have the following code but it isn't working as expected:

 

Then show some input data and the expected output.

 

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 281 views
  • 0 likes
  • 4 in conversation