BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Reading Data with spaces

data l;
input no address city age ;
cards;
1, main road near hospital ,mumbai, 30
2,govt area ,pune,38
3,central area,near adminstrative building ,Patna,40

in the observation there are some spaces and i cannot decalre the length as the length is differ from one observation to another.how can i read it.and in the third observation there is a comma in the observation also


Output

no address city age

1 main road near hospital mumbai 30
2 govt area pune 38
3 central area near adminstrative building Patna 40
1 REPLY 1
DanielSantos
Barite | Level 11
Hi.

For reading data through cards, with delimited data, this would suffice:

data l;
length NO 8 ADDRESS $200 CITY $50 AGE 8;
input; * read line;

* split line;
NO=inputn(scan(_infile_,1,','),'best.');
CITY=reverse(scan(reverse(_infile_),2,','));
AGE=inputn(reverse(scan(reverse(_infile_),1,',')),'best.');
ADDRESS=scan(_infile_,2,',');
cards;
1, main road near hospital ,mumbai, 30
2,govt area ,pune,38
3,central area,near adminstrative building ,Patna,40
run;

But this code will not have the desired behavior for the 3rd row.

For this, use regular expression parsing:

data l;
length NO 8 ADDRESS $200 CITY $50 AGE 8;
input;
drop _:;
* prepare reg. expression for text parsing;
_EXPR='s/([ 0-9]*),(.*),(.*),([ 0-9]*)/\1#\2#\3#\4/i';
_REGX=prxparse(_EXPR);
_PRX=prxchange(_REGX,1,_infile_);

* split parsed text into desired variables;
NO=inputn(cats(scan(_PRX,1,'#')),'best.');
ADDRESS=cats(scan(_PRX,2,'#'));
CITY=cats(scan(_PRX,3,'#'));
AGE=inputn(cats(scan(_PRX,4,'#')),'best.');
cards;
1, main road near hospital ,mumbai, 30
2,govt area ,pune,38
3,central area,near adminstrative building ,Patna,40
run;

Check here for regular expression info:
http://www.regular-expressions.info/tutorial.html

Check here for the regular expression functions online documentation:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002295977.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 550 views
  • 0 likes
  • 2 in conversation