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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

2 REPLIES 2
Reeza
Super User

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

 

 

 


 

 

Astounding
PROC Star

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

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.

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
  • 2 replies
  • 703 views
  • 3 likes
  • 3 in conversation