DATA Step, Macro, Functions and more

Please help with this small code.

Accepted Solution Solved
Reply
Regular Contributor
Regular Contributor
Posts: 166
Accepted Solution

Please help with this small code.

Hi guys,

I am trying to create a fixed width output file with the below code

data _null_;
file PRINT;

do UNTIL (_N_ = EOF);
set sashelp.class(where=(sex='M')) end=eof;
put @01 sex $12.
@3 name $11.
;
END;
put 'break here part 2';
DO UNTIL (_N_ = (EOF+1+EFF));
set sashelp.class(where=(sex='F')) end=eof;
put @01 sex $12.
@3 name $11.
    ;
END;
run;

For the above code i get the correct ouput. But if i was to change this with the code below . Then no output generates because the SEX = 'FM' is not there in the table. but i was hoping that atleast the sex='F' values should get printed in the output.

data _null_;
file PRINT;

do UNTIL (_N_ = EOF);
set sashelp.class(where=(sex='FM')) end=eof;
put @01 sex $12.
@3 name $11.
;
END;
put 'break here part 2';
DO UNTIL (_N_ = (EOF+1+EFF));
set sashelp.class(where=(sex='F')) end=eof;
put @01 sex $12.
@3 name $11.
    ;
END;
run;

Please do guide.


Accepted Solutions
Solution
‎09-05-2011 01:25 PM
PROC Star
Posts: 7,366

Please help with this small code.

There are numerous ways.  e.g.:

data class;

  set sashelp.class (where=(sex eq 'F'));

run;

data _null_;

  file print;

  do UNTIL (EOF);

    set class end=eof;

    if sex eq 'M' then do;

      put @01 sex $12.

      @3 name $11.

      ;

    END;

  end;

  put 'break here part 2';

  DO UNTIL ((EFF));

    set class(where=(sex='F')) end=eff;

      x=_n_;

      put @01 sex $12.

          @3 name $11.

       ;

  END;

run;

Does that accomplish what you want?

View solution in original post


All Replies
PROC Star
Posts: 7,366

Please help with this small code.

What are you actually trying to do?  Plus, your code has some unusual uses and one undeclared variable.

_N_ will always be equal to 1 and EFF has never been initialized.  Are you trying to do something like:

data _null_;

  file print;

  do UNTIL (EOF);

    set sashelp.class(where=(sex in ('M','F'))) end=eof;

    put @01 sex $12.

          @3 name $11.

        ;

  END;

  put 'break here part 2';

  DO UNTIL ((EFF));

    set sashelp.class(where=(sex='F')) end=eff;

    x=_n_;

    put @01 sex $12.

          @3 name $11.

        ;

  END;

run;

Regular Contributor
Regular Contributor
Posts: 166

Please help with this small code.

Hi Art,

My aim is to create a fixedwidth file in the following manner

first print the sex and names of all males

second put a line "break here part 2"

third print the sex and names of all females.

What you have given is absolutely correct. But my problem arises if there are no Males in my data then the data for the females also doesnot get printed.

So please guide on how to take this forward.

Example

data test;

set sashelp.class;

where sex = 'F';

run;

data _null_;

  file print;

  do UNTIL (EOF);

    set test(where=(sex ='M')) end=eof;

    put @01 sex $12.

          @3 name $11.

        ;

  END;

  put 'break here part 2';

  DO UNTIL ((EFF));

    set test(where=(sex='F')) end=eff;

    x=_n_;

    put @01 sex $12.

          @3 name $11.

        ;

  END;

run;

Solution
‎09-05-2011 01:25 PM
PROC Star
Posts: 7,366

Please help with this small code.

There are numerous ways.  e.g.:

data class;

  set sashelp.class (where=(sex eq 'F'));

run;

data _null_;

  file print;

  do UNTIL (EOF);

    set class end=eof;

    if sex eq 'M' then do;

      put @01 sex $12.

      @3 name $11.

      ;

    END;

  end;

  put 'break here part 2';

  DO UNTIL ((EFF));

    set class(where=(sex='F')) end=eff;

      x=_n_;

      put @01 sex $12.

          @3 name $11.

       ;

  END;

run;

Does that accomplish what you want?

Occasional Contributor
Posts: 16

Please help with this small code.

Wouldn't it be simpler to just sort the data set first and then use the first. and last. options? E.g.:

PROC SORT DATA=SASHELP.CLASS OUT=CLASS;

    BY DESCENDING SEX ;

RUN;

DATA_NULL_;

    SET CLASS;

    BY DESCENDING SEX ;

    IF FIRST.SEX AND SEX='F'THEN put'break here part 2';

    put @01 sex $12.@3 name $11.;

RUN;

Regular Contributor
Posts: 184

Please help with this small code.

Make the first loop conditional, as in

   IF NOT EOF THEN do UNTIL (EOF);

Also, you will probably want a STOP statement at the end.

Regular Contributor
Regular Contributor
Posts: 166

Please help with this small code.

Thanks a lot Art.This was extremely simple. Dont know why i keep complicating things.

Hi Hobbes,

Thanks for your response What you have given should work too.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 225 views
  • 3 likes
  • 4 in conversation