Help using Base SAS procedures

IF statement

Reply
Regular Contributor
Posts: 161

IF statement

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;
Valued Guide
Posts: 634

Re: IF statement

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]
Regular Contributor
Posts: 161

Re: IF statement

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
Super User
Posts: 11,343

Re: IF statement

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;
Regular Contributor
Posts: 161

Re: IF statement

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
Valued Guide
Posts: 634

Re: IF statement

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?
Super User
Posts: 10,028

Re: IF statement

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
Ask a Question
Discussion stats
  • 6 replies
  • 405 views
  • 0 likes
  • 4 in conversation