Help using Base SAS procedures

Reg Data Reading

Reply
N/A
Posts: 0

Reg Data Reading

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
Super Contributor
Posts: 474

Re: Reg Data Reading

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.
Ask a Question
Discussion stats
  • 1 reply
  • 92 views
  • 0 likes
  • 2 in conversation