BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10
data bat;
do i=1 to 3;
com2="rem@echo off";
com="call nmgo" || compress("TEST"||i);
com1="call nmgo" || compress("TEST"||i) ||COMPRESS(".CTL -MAXLIM=3"||""); 
output;
end;
run;
data _null_;
  set bat;
  file '/folders/myfolders/BOOTSTRAP_PED/BATFILEPREP/booti.bat';
  put @1 com2 ; 
  put @2 com1;
run;

I have the attached code which works for what I want to do.  However for the put statement I need to limit the rem@echo off to the first line of booti.  What I have is this with rem@echo off in every other line:

rem@echo off
CALL NMGO TEST1.CTL -MAXLIM=3

rem@echo off
CALL NMGO TEST2.CTL -MAXLIM=3

but what I want is this for the rem@echo off to appear in only line 1:

rem@echo off
CALL NMGO TEST1.CTL -MAXLIM=3
CALL NMGO TEST2.CTL -MAXLIM=3
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If I understand what you want try

 

data _null_;
  set bat;
  file '/folders/myfolders/BOOTSTRAP_PED/BATFILEPREP/booti.bat';
  If _n_ = 1 then put @1 com2 ; 
  put @2 com1;
run;

_n_ is an automatic variable that SAS increments at each "loop" through the data step. In this case it is, in effect, the "line read count".

So test that variable and only create the output the first time.

 

Unless you need that BAT data set elsewhere this could be done with

 

data _null_;
   file 'your file';
   put @1 "rem@echo off";
   do i=1 to 3;
      com="call nmgo" || compress("TEST"||i);
      com1="call nmgo" || compress("TEST"||i) ||COMPRESS(".CTL -MAXLIM=3"||""); 
      put @2 com1;
   end;
run;

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

You can test the automatic iteration counter, _N_, to know when you are on the first iteration of the data step.

Do you want the space after NMGO that appears in the example output or not?

data bat;
  com2="rem@echo off";
  do i=1 to 3;
    com ='call nmgo' || cats("TEST",i);
    com1='CALL NMGO ' || cats("TEST",i,'.CTL -MAXLIM=3);
    output;
  end;
run;
data _null_;
  set bat;
  file '/folders/myfolders/BOOTSTRAP_PED/BATFILEPREP/booti.bat';
  if _n_=1 then put com2 ; 
  put @2 com1;
run;
jacksonan123
Lapis Lazuli | Level 10
When I ran the code I got this error in the log.

data bat;

75 com2="rem@echo off";

76 do i=1 to 3;

77 com='call NMGO' || cats ("TEST",i);

78 com1='call NMGO' || cats ("TEST",i,'.CTL -MAXLIM=3);

79 output;

80 end;

81 run;

82 data _null_;

83 set bat;

84 file
'/folders/myfolders/BOOTSTRAPAPTENSIO_PED/BATFILEPREP/boot.bat';

85 IF _N_=1 THEN put com2 ;

86 put @2 com1;

84 file
'/folders/myfolders/BOOTSTRAPAPTENSIO_PED/BATFILEPREP/boot.bat';


________

557

ERROR 557-185: Variable boot is not an object.

87 run;

88

89 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

90 ODS HTML CLOSE;

91 &GRAPHTERM; ;*';*";*/;RUN;QUIT;

ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION
phase.


ballardw
Super User

Please post log entries in a code box. By now you should have realized that the message windows reformat the text and make the diagnostic characters that SAS supplies appear in the wrong location as well as sometimes inserting extra newline characters.

jacksonan123
Lapis Lazuli | Level 10
I forgot about doing that, next time I will to avoid that mistake.
Tom
Super User Tom
Super User

You are missing at least one closing quote mark in this line:

78 com1='call NMGO' || cats ("TEST",i,'.CTL -MAXLIM=3);
jacksonan123
Lapis Lazuli | Level 10
Your solution also works after I put in the missing (').

Thanks for the help and it is accepted as a solution.
ballardw
Super User

If I understand what you want try

 

data _null_;
  set bat;
  file '/folders/myfolders/BOOTSTRAP_PED/BATFILEPREP/booti.bat';
  If _n_ = 1 then put @1 com2 ; 
  put @2 com1;
run;

_n_ is an automatic variable that SAS increments at each "loop" through the data step. In this case it is, in effect, the "line read count".

So test that variable and only create the output the first time.

 

Unless you need that BAT data set elsewhere this could be done with

 

data _null_;
   file 'your file';
   put @1 "rem@echo off";
   do i=1 to 3;
      com="call nmgo" || compress("TEST"||i);
      com1="call nmgo" || compress("TEST"||i) ||COMPRESS(".CTL -MAXLIM=3"||""); 
      put @2 com1;
   end;
run;
jacksonan123
Lapis Lazuli | Level 10
Works well.

Thanks for the help

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
  • 8 replies
  • 778 views
  • 0 likes
  • 3 in conversation