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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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