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.
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?
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;
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;
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?
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;
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.
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.
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 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.
Ready to level-up your skills? Choose your own adventure.