BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Gilles-Protais
Obsidian | Level 7

Thank you very much.

It Worked for me now.

Tom
Super User Tom
Super User

This has been asked before.  You could try search for the previous questions and answers.

 

In general there are two methods.

 

1) Make version of the text file without the NA strings.  Then use what every method you want to read the converted file.

 

Example:

filename csv 'my original file.csv';
filename fixed 'my original file fixed.csv';
data _null_;
  infile csv ;
  file fixed;
  input ;
  _infile_=tranwrd(cats(',',_infile_,','),',NA,',',,');
  _infile_=substrn(_infile_,2,length(_infile_)-2);
  put _infile_;
run;

2) Write your own data step to read the data instead of forcing SAS to GUESS how to read the file.

 

If you write your own data step you get to decide which variables are numeric and which are character.  If you don't do anything else then SAS will write a note when it sees one of those NA strings in a field being read as a numeric value, but the result will be a missing value, which is what you want.

 

Reading a CSV file is TRIVIAL with a data step.  So there is no need to resort to forcing SAS to GUESS how to read it. Just define the variables. Attach any NEEDED formats or informats.  Then read the variables.

 

Example:

data want;
  infile csv dsd truncover firstobs=2;
  length id $10 age 8 start_date 8 ;
  informat start_date yymmdd. ;
  format start_date yymmdd10.;
  input id -- start_date ;
run;

To suppress the notes about invalid values you could use the ?? modifier on the INPUT statement.  But note that will suppress ALL of the warnings about invalid values.  So values like FRED will be silently set to missing just like values like NA.

 

Of you could define a special INFORMAT that will convert the NA strings to missing and then use normal informat for the other values.  

proc format ;
invalue na     'NA','na','Na' = . other = [32.] ;
invalue na_ymd 'NA','na','Na' = . other = [yymmdd10.] ;
run;

Then use the informat in the data step.

data want;
  infile csv dsd truncover firstobs=2;
  length id $10 age 8 start_date 8 ;
  informat start_date na_ymd.  age na.;
  format start_date yymmdd10.;
  input id -- start_date ;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 16 replies
  • 2696 views
  • 0 likes
  • 3 in conversation