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
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;
Try this change double dash --
array codebook [10] DPQ010--DPQ100;
When I submitted the array with double dash I received these errors in my log:
That was a typo on my part. All should be DPQ. Fixing that still gives me the following errors:
can you post your code plz
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;
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;
Thanks for pointing out @mkeintz . Wondering how i missed that one.
This works thank you so much!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.