BookmarkSubscribeRSS Feed
Rafi
Fluorite | Level 6

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;

4 REPLIES 4
LinusH
Tourmaline | Level 20

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 965 views
  • 0 likes
  • 5 in conversation