BookmarkSubscribeRSS Feed
R_A_G_
Calcite | Level 5
Hello,

I have a code to read a statement from a text file but sometimes there is no text to read, which in that case I need to insert 0 for each one them. Would someone help me figure out how to do that.

Thanks

Code:
data CDM1.temp_&seed;
infile "c:\CDM1\&commandfile..out" truncover;
input test $21. @;
if test="Information Criteria" then do;
input / / @40 num_fre_par
/ @40 akaike5
/ @40 bayesian
/ @40 sam_siz_adj_bic ;

else input num_fre_par=0 akaike5=0 bayesian=0; this doesn't give me 0 for each missing statement
output;
stop;
end;
drop test;
run;
6 REPLIES 6
ArtC
Rhodochrosite | Level 12
I am not sure what you are trying to do here. Is there only one incoming row? or do you want to issue the STOP only if there is no information?

Let me take a guess and we can refine the answer:
[pre]
data CDM1.temp_&seed;
infile "c:\CDM1\&commandfile..out" truncover;
input test $21. @;

if test="Information Criteria" then do;
* this is good data - read it;
input / / @40 num_fre_par
/ @40 akaike5
/ @40 bayesian
/ @40 sam_siz_adj_bic ;
output;
end; * add the END;
else do;
* set values to missing and stop reading data;
num_fre_par=0; akaike5=0; bayesian=0;
output;
stop;
end;
drop test;
run;
[/pre]
R_A_G_
Calcite | Level 5
Hello ArtC,

This looked like it would take care of my problem except that even with the OUTPUT; code it still replaces all the data with "0" instead of just plugging in "0" for the missing data file only.

Just to clarify what I am trying to do: I got 150 files that is several pages long but I only need this section of the data "Information Criteria" except that some of the files do not have this data. So I would like to plug in "0" for the data files that do not have this information. This way I have for example :Temp_1 with all 4 variables and Temp_1 with the value of "0" for all the variables.

Thank you
ballardw
Super User
You have a couple problems.

First your "DO" clause includes the "ELSE" so it never gets executed. You only want an input to clear the hold from the "@".

Try Instead:
if test="Information Criteria" then do;
input / / @40 num_fre_par
/ @40 akaike5
/ @40 bayesian
/ @40 sam_siz_adj_bic ;
end;
Else do;
num_fre_par=0;
akaike5=0;
bayesian=0;
input;
end;
R_A_G_
Calcite | Level 5
Sorry but it does not work. It seems like ArtC's should work, if I could make the files keep their value instead of replacing all with "0"-missing.

Just to clarify what I am trying to do: I got 150 files that is several pages long but I only need this section of the data "Information Criteria" except that some of the files do not have this data -missing. So I would like to plug in "0" for the data files that do not have this information. This way I have for example :Temp_1 with all 4 variables and Temp_2 with the value of "0" for all the variables. At the end I will end up stacking all these 150 data on top each other in ORDER-that is another issue I am having.

Thank you
ArtC
Rhodochrosite | Level 12
You say that you have 150 'files'. This data step is processing one file at a time, and it creates one data set at a time. For a given file what do you want to have happen? This DATA step creates a single observation data set under certain conditions. Do you want all 150 files to be concatenated into a single data set - with this 'special condition' being just a row in that data set?
Ksharp
Super User
I have a solution If you want.
[pre]



data temp1;
infile 'c:\log.txt' length=len;
input row $varying200. len;
run;
data temp1(drop=row);
set temp1 end=last;
retain found 'N';
if scan(row,1) eq 'NOTE:' then do;
name=scan(row,2); found='Y';
output;
end;
if last and found='N' then do;
name='0';
output;
end;
run;
[/pre]


Ksharp

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1703 views
  • 0 likes
  • 4 in conversation