Moving part of an observation to a new variable column.

Reply
Occasional Contributor
Posts: 6

Moving part of an observation to a new variable column.

Hey guys

 

Quick question -

 

I have a variable that has observations like auckland 1010 and I want to move the 1010 to a new variable name POSTCODE.

 

Is this possible, and if so how?

 

Many thanks

Joshua

Super User
Super User
Posts: 7,042

Re: Moving part of an observation to a new variable column.

Posted in reply to joshua_james

newvar = scan(oldvar,-1,' ');

 

Trusted Advisor
Posts: 1,228

Re: Moving part of an observation to a new variable column.

Posted in reply to joshua_james

or using compress function:

 

data have;
input var_name $20.;
POSTCODE=compress(var_name,,'kd');
datalines;
auckland 1010
;

Respected Advisor
Posts: 4,920

Re: Moving part of an observation to a new variable column.

Posted in reply to joshua_james

If you only want proper NZ postcodes:

 

data test;
input address $64.;
datalines;
Auckland  1112
Christchurch 8110
Christchurch NZ
;


data codes;
set test;
m = prxMatch("/\b\d{4}\s*$/", address);
if m > 0 then postcode = substr(address, m, 4);
drop m;
run;

proc print; run;
PG
Ask a Question
Discussion stats
  • 3 replies
  • 299 views
  • 0 likes
  • 4 in conversation