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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 670 views
  • 0 likes
  • 2 in conversation