DATA Step, Macro, Functions and more

sas data input

Reply
Occasional Contributor
Posts: 5

sas data input

I have the below code. 

 

The variable State has few values with space in between (eg: South Carolina). I want to read those values without modifying the data but modifying the input statement. Any kind of suggestion is appreciated.

 

data prog;
input State $ EnterDate Size;
datalines;
Delaware 07DEC1787 1955
Pennsylvania 12DEC1787 44820
New Jersey 18DEC1787 7418
Georgia 02JAN1788 57918
Connecticut 09JAN1788 4845
Massachusetts 06FEB1788 7838
Maryland 28APR1788 9775
South Carolina 23MAY1788 30111
;
run;

Super User
Posts: 5,424

Re: sas data input

In your real data, are you sure that you have a space (with the same HEX code) as delimiter?

If yes, try to get new data, one format is with all char values embedded within double quotes.

If that's not possible, I would read the whole record into one variable, then search for the start position of your date, and assign preceding string to state.

Data never sleeps
Super User
Super User
Posts: 7,942

Re: sas data input

You can't.  There is no logical way of knowing if a space is related to the text, or as a delimiter.  Now you could process the imported string, however I would suggest you get the file sent in a decent robust format such as CSV.

Super User
Posts: 5,497

Re: sas data input

As others have suggested, it might be easier to modify the data.  But it can be done.  For example:

 

data prog;

length state1 state2 $ 15 state $ 20;

informat EnterDate date9.;

input @;

if countw(_infile_)=4 then do;

   input state1 $ state2 $ EnterDate  size;

   state = trim(state1) || ' ' || state2;

end;

else input state $ EnterDate size;

format EnterDate yymmdd10.;

drop state1 state2;
datalines;
Delaware 07DEC1787 1955
Pennsylvania 12DEC1787 44820
New Jersey 18DEC1787 7418
Georgia 02JAN1788 57918
Connecticut 09JAN1788 4845
Massachusetts 06FEB1788 7838
Maryland 28APR1788 9775
South Carolina 23MAY1788 30111
;

 

Notice that you cannot read EnterDate as a numeric variable unless you apply an informat.

 

Also note, this code is untested.  It's probably fine as is, but may require small changes.

 

Good luck.

Super User
Super User
Posts: 7,039

Re: sas data input

If you can controll the formatting of the input file then force there to be two spaces between the state name and the date. Then you can use the & modifier on the input statement. Otherwise you will need to add some logic. In this case since state names do not contain digits and your date values always start with a digit it is easy.

 

data prog;
  length State $20 EnterDate Size 8;
  input State $20. @;
  x=indexc(state,'0123456789');
  if x>0 then state=substr(state,1,x-1);
  else x=length(state)+2;
  input @x EnterDate date9. size ;
  format enterdate date9. size comma7.;
  drop x;
datalines;
Delaware 07DEC1787 1955
Pennsylvania 12DEC1787 44820
New Jersey 18DEC1787 7418
Georgia 02JAN1788 57918
Connecticut 09JAN1788 4845
Massachusetts 06FEB1788 7838
Maryland 28APR1788 9775
South Carolina 23MAY1788 30111
;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 186 views
  • 0 likes
  • 5 in conversation