BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
torvin10
Calcite | Level 5

Hi all

 

Im having trouble creating a dataset from a txt.file, where * and $ is amongst my variables.

The data: 

(ii);
A123  4Mar1989  8,6,00
***************
    A037 23Jun1957  21,450
**************
 M015 19Sep1977$17,500
***********

 Code so far:

data ii;
  input DEPT $1. @1 ID $ BIRTHDAY :date9. +(-5) YEAR :8. SALARY comma10.0;
  datalines;
A123 4Mar1989 8,6,00
***************
A037 23Jun1957 21,450
**************
M015 19Sep1977$17,500
*********** ; run;

However this loads the *** and the $ as missing characters. 

The first record should be:

ID = "A123", DEPT = "A", BIRTHDAY = 10655, YEAR = 1989, SALARY = 8600 

 Any suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions
MichaelLarsen
SAS Employee

Here is my solution

data ii;
  /* Input the record into memory (_INFILE_) but do not release it */
  input @; 
  /* Remove the $ sign from the record */
  _infile_ = translate(_infile_,' ','$');
  /* Read the record again and skip the ***** line using the slash sign (/) */
  input ID $ BirthDay :date9. Salary comma10.0 /;
  /* Create derived variables */
  Dept = substr(ID,1,1);
  Year = year(BirthDay);
  format BirthDay date9. Salary comma10.0 ;
  datalines;
A123 4Mar1989 8,6,00
***************
 A037 23Jun1957 21,450
**************
 M015 19Sep1977$17,500
***********
;
run;

I am assuming that every other line is a line with only ***************

View solution in original post

2 REPLIES 2
MichaelLarsen
SAS Employee

Here is my solution

data ii;
  /* Input the record into memory (_INFILE_) but do not release it */
  input @; 
  /* Remove the $ sign from the record */
  _infile_ = translate(_infile_,' ','$');
  /* Read the record again and skip the ***** line using the slash sign (/) */
  input ID $ BirthDay :date9. Salary comma10.0 /;
  /* Create derived variables */
  Dept = substr(ID,1,1);
  Year = year(BirthDay);
  format BirthDay date9. Salary comma10.0 ;
  datalines;
A123 4Mar1989 8,6,00
***************
 A037 23Jun1957 21,450
**************
 M015 19Sep1977$17,500
***********
;
run;

I am assuming that every other line is a line with only ***************

Kurt_Bremser
Super User

And if the occurence of the ****** lines is irregular, here a slight variation of @MichaelLarsen's code:

data ii;
  /* Input the record into memory (_INFILE_) but do not release it */
  input @; 
  if not index(_infile_,'****');
  /* Remove the $ sign from the record */
  _infile_ = translate(_infile_,' ','$');
  /* Read the record again */
  input ID $ BirthDay :date9. Salary comma10.0;
  /* Create derived variables */
  Dept = substr(ID,1,1);
  Year = year(BirthDay);
  format BirthDay date9. Salary comma10.0 ;
  datalines;
A123 4Mar1989 8,6,00
***************
 A037 23Jun1957 21,450
**************
 M015 19Sep1977$17,500
***********
;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3406 views
  • 0 likes
  • 3 in conversation