BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
smart
Fluorite | Level 6

My Goal:

Create a temporary data set called Depression that reads the dpq_h_errorsSAS data set and changes all impossible values in the depression variables to missing. The dpq_h_errors data set is stored in the /courses/ddb976e5ba27fe300/NHANES 2013_2014 folder.

 

My code:


libname in '/courses/ddb976e5ba27fe300/NHANES 2013_2014';

 

data Depression;
input SEQN DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100;
array codebook [10] DPQ010-DPQ100;
do i = 1 to 10;
if codebook [i] = 10 then codebook[i] = 0;
end;

proc contents data=in.dpq_h_errors;
run;

proc print data=in.dpq_h_errors (obs=10);
run;

 

The error and warning I am getting:

ERROR: Too many variables defined for the dimension(s) specified for the array codebook

 

WARNING: The data set WORK.DEPRESSION may be incomplete. When this step was stopped there were 0 observations and 12 variables.
WARNING: Data set WORK.DEPRESSION was not replaced because this step was stopped.
1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

The column names referred in the array statement should be  be contiguous to use the reference like DPQ010-DPQ100. In your case the columns are  DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100 which are not contiguous. Moreover DPQ010-DPQ100 are seen as 90 elements by sas and the array is declared to have only 10 elements.

 

You have to expand the column list , which is simplified in this example.

 

data have;
input SEQN DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100;
cards;
1 10 11 12 13 14 15 16 17 18 19 20
run;

proc contents data=have out=temp(keep=name) noprint;
run;

Proc sql noprint;
	select name into :arr_lst separated by ' '
		from temp
		where name like 'DP%';
quit;

%put &=arr_lst;

data Depression;
	set have;
	array codebook [10] &arr_lst;

	do i = 1 to 10;
		if codebook [i] = 10 then
			codebook[i] = 0;
	end;
run;

View solution in original post

9 REPLIES 9
novinosrin
Tourmaline | Level 20

Try this change double dash --

array codebook [10] DPQ010--DPQ100;
smart
Fluorite | Level 6

When I submitted the array with double dash I received these errors in my log:

 

 

data Depression;
74 input SEQN DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100;
75 array codebook [10] DPQ010--DPQ100
76 do i = 1 to 10;
_ __
22 22
200 200
ERROR: DPQ100 does not follow DPQ010 on the list of previously defined variables.
ERROR: Too few variables defined for the dimension(s) specified for the array codebook.
ERROR 22-322: Syntax error, expecting one of the following: a name, (, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
 
ERROR 200-322: The symbol is not recognized and will be ignored.
 
77 if codebook [i] = 10 then codebook[i] = 0;
78 end;
___
161
ERROR 161-185: No matching DO/SELECT statement.
 
novinosrin
Tourmaline | Level 20
74 input SEQN DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100;/*all DP series*/
75 array codebook [10] DPQ010--DPQ100  /*DPQ?????????*/
 
wouldn't the array statement be
 
 array codebook [10] DPQ010--DP100 rather?
 
smart
Fluorite | Level 6

That was a typo on my part. All should be DPQ. Fixing that still gives me the following errors:

 

ERROR: Too many variables defined for the dimension(s) specified for the array codebook.
ERROR 22-322: Syntax error, expecting one of the following: a name, (, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
 
ERROR 200-322: The symbol is not recognized and will be ignored.
 
77 if codebook [i] = 10 then codebook[i] = 0;
78 end;
___
161
ERROR 161-185: No matching DO/SELECT statement.
novinosrin
Tourmaline | Level 20

can you post your code plz

r_behata
Barite | Level 11

The column names referred in the array statement should be  be contiguous to use the reference like DPQ010-DPQ100. In your case the columns are  DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100 which are not contiguous. Moreover DPQ010-DPQ100 are seen as 90 elements by sas and the array is declared to have only 10 elements.

 

You have to expand the column list , which is simplified in this example.

 

data have;
input SEQN DPQ010 DP020 DP030 DP040 DP050 DP060 DP070 DP080 DP090 DP100;
cards;
1 10 11 12 13 14 15 16 17 18 19 20
run;

proc contents data=have out=temp(keep=name) noprint;
run;

Proc sql noprint;
	select name into :arr_lst separated by ' '
		from temp
		where name like 'DP%';
quit;

%put &=arr_lst;

data Depression;
	set have;
	array codebook [10] &arr_lst;

	do i = 1 to 10;
		if codebook [i] = 10 then
			codebook[i] = 0;
	end;
run;
mkeintz
PROC Star

 

You are doing a very roundabout way to get a list of variable names, based on the first 2 letters of those name.

 

This single step does what I think you want.

data depression;
  set have;
  array codebook {*} dp: ;
  do i=1 to dim(codebook);
    if codebook{i}=10 then codebook{i}=0;
  end;
run;

 

  1.  this uses the expression DP:   to indicate all vars whose names begin with DP.
  2. It uses an asterisk in the braces, telling sas to figure out how many elements are in the array
  3. And it uses dim(codebook) instead of 10 as the do loop upper limit.  There's no need for you to know the size in advance.
--------------------------
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

--------------------------
r_behata
Barite | Level 11

Thanks for pointing out @mkeintz . Wondering how i missed that one.

smart
Fluorite | Level 6

This works thank you so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 9 replies
  • 5568 views
  • 2 likes
  • 4 in conversation