Hi all,
I used the following code to replace numeric missing with 0 in 52 items. this procedure is repeated for 10 times. however, in the final saved output, the field of the 52 items is blank. only id and bucket variable are there. What is the problem? Thanks!
%macro missing (form,group,percent,type);
%do i=1 %to 10;
FILENAME datain "&path.F&Form._m&percent..rep&i..txt";
FILENAME out_r "&pathout.F&Form._m&percent..rep&i..txt";
/*-------------------------------read in data file--------------------------------------------------------*/
data F&Form._m&percent.rep&i.;
infile datain;
input @1 ID 4.
@6 (i1-i&TotalItems) (1.)
@59 bucket 3.;
;
run;
proc sql noprint;
select count(bucket) into : loopnum from F&Form._m&percent.rep&i. where bucket=0;
quit;
%put "the number of loop is:&loopnum.";
/*-------------- generate missing for bucket=0 students --------------------------------------------------*/
data F&Form._&type._m&percent.rep&i.;
set F&Form._m&percent.rep&i.;
array iVars i1-i52;
array idx idx1-idx48;
if _N_<=&loopnum. then do until (cmiss(of iVars[*])=48);
call missing(iVars[ceil(52*rand('uniform'))]);
end;
drop idx:;
run;
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
%if bucket=0 %then %do;
%do j=1 %to 52;
array items i1-i52;
array que it1-it52;
%if items[j]=. %then que[j]=0;
%else que[j]=items[j];
%end;
%end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
%end;
%mend missing;
%missing (form=A, group=100,percent=1,type=&type.);
You are using far too much macro language in this step:
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
%if bucket=0 %then %do;
%do j=1 %to 52;
array items i1-i52;
array que it1-it52;
%if items[j]=. %then que[j]=0;
%else que[j]=items[j];
%end;
%end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
Basically, macro language examines whether these letters: "bucket" are equal to these letters: "0". Since they are not equal, none of the macro language statements execute. The next statement in your DATA step is the FILE statement, and PUT writes out messages with no changes to the data.
Remove all macro language from statements related to data manipulation:
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
if bucket=0 then do;
array items i1-i52;
array que it1-it52;
do j=1 to 52;
if items[j]=. then que[j]=0;
else que[j]=items[j];
end;
end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
Let the DATA step manipulate data. That's what it is there for. This doesn't mean your program will be error-free, but at least the DATA step will see the data manipulation statements and try to execute them.
Run your program with the debugging options (first line in code below) and show the log for the first two loops. Also, please post your code using a code snippet box. It makes it much easier to see if there's syntax error than when in HTML editor.
Like this:
options mprint symbolgen mlogic; %macro missing (form, group, percent, type); %do i=1 %to 10; FILENAME datain "&path.F&Form._m&percent..rep&i..txt"; FILENAME out_r "&pathout.F&Form._m&percent..rep&i..txt"; /*-------------------------------read in data file--------------------------------------------------------*/ data F&Form._m&percent.rep&i.; infile datain; input @1 ID 4. @6 (i1-i&TotalItems) (1.) @59 bucket 3.; ; run; proc sql noprint; select count(bucket) into : loopnum from F&Form._m&percent.rep&i. where bucket=0; quit; %put "the number of loop is:&loopnum."; /*-------------- generate missing for bucket=0 students --------------------------------------------------*/ data F&Form._&type._m&percent.rep&i.; set F&Form._m&percent.rep&i.; array iVars i1-i52; array idx idx1-idx48; if _N_<=&loopnum. then do until (cmiss(of iVars[*])=48); call missing(iVars[ceil(52*rand('uniform'))]); end; drop idx:; run; data _null_; set F&Form._&type._m&percent.rep&i.; %if bucket=0 %then %do; %do j=1 %to 52; array items i1-i52; array que it1-it52; %if items[j]=. %then que[j]=0; %else que[j]=items[j]; %end; %end; FILE out_r lrecl=5000 nopad DLM=''; PUT @1 ID 4. @6 (it1-it&TotalItems) (1.) @59 bucket 3.; run; %end; %mend missing; %missing (form=A, group=100, percent=1, type=&type.);
@TX_STAR wrote:
Hi all,
I used the following code to replace numeric missing with 0 in 52 items. this procedure is repeated for 10 times. however, in the final saved output, the field of the 52 items is blank. only id and bucket variable are there. What is the problem? Thanks!
%macro missing (form,group,percent,type);
%do i=1 %to 10;
FILENAME datain "&path.F&Form._m&percent..rep&i..txt";
FILENAME out_r "&pathout.F&Form._m&percent..rep&i..txt";
/*-------------------------------read in data file--------------------------------------------------------*/
data F&Form._m&percent.rep&i.;
infile datain;
input @1 ID 4.
@6 (i1-i&TotalItems) (1.)
@59 bucket 3.;
;
run;
proc sql noprint;
select count(bucket) into : loopnum from F&Form._m&percent.rep&i. where bucket=0;
quit;
%put "the number of loop is:&loopnum.";
/*-------------- generate missing for bucket=0 students --------------------------------------------------*/
data F&Form._&type._m&percent.rep&i.;
set F&Form._m&percent.rep&i.;
array iVars i1-i52;
array idx idx1-idx48;
if _N_<=&loopnum. then do until (cmiss(of iVars[*])=48);
call missing(iVars[ceil(52*rand('uniform'))]);
end;
drop idx:;
run;
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
%if bucket=0 %then %do;
%do j=1 %to 52;
array items i1-i52;
array que it1-it52;
%if items[j]=. %then que[j]=0;
%else que[j]=items[j];
%end;
%end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
%end;
%mend missing;
%missing (form=A, group=100,percent=1,type=&type.);
You are using far too much macro language in this step:
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
%if bucket=0 %then %do;
%do j=1 %to 52;
array items i1-i52;
array que it1-it52;
%if items[j]=. %then que[j]=0;
%else que[j]=items[j];
%end;
%end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
Basically, macro language examines whether these letters: "bucket" are equal to these letters: "0". Since they are not equal, none of the macro language statements execute. The next statement in your DATA step is the FILE statement, and PUT writes out messages with no changes to the data.
Remove all macro language from statements related to data manipulation:
data _null_ ;
set F&Form._&type._m&percent.rep&i.;
if bucket=0 then do;
array items i1-i52;
array que it1-it52;
do j=1 to 52;
if items[j]=. then que[j]=0;
else que[j]=items[j];
end;
end;
FILE out_r lrecl=5000 nopad DLM='';
PUT
@1 ID 4.
@6 (it1-it&TotalItems) (1.)
@59 bucket 3.;
run ;
Let the DATA step manipulate data. That's what it is there for. This doesn't mean your program will be error-free, but at least the DATA step will see the data manipulation statements and try to execute them.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.