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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

6 REPLIES 6
art297
Opal | Level 21

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;

NN
Quartz | Level 8 NN
Quartz | Level 8

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;

art297
Opal | Level 21

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?

Hobbes
Calcite | Level 5

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;

Howles
Quartz | Level 8

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.

NN
Quartz | Level 8 NN
Quartz | Level 8

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.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1313 views
  • 3 likes
  • 4 in conversation