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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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