DATA Step, Macro, Functions and more

extract numbers in a text field

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

extract numbers in a text field

Hi,

I have a variable "datetext" that contains a set of numbers separated by "/".  

 

for example patient A has datetext="2130/3457/4732"

each of the 3 numbers is the number of days of patient A to an event, so patient A has this event 3 times.

 

I want to extract each of the 3 numbers as a number variable so that for patient A:

date1=2130, date2=3457 and date3=4732

 

many thanks for your help

 

Raymond

 

 

 


Accepted Solutions
Solution
‎01-28-2018 10:41 AM
New Contributor
Posts: 2

Re: extract numbers in a text field

[ Edited ]
Posted in reply to novinosrin
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;

View solution in original post


All Replies
Super User
Posts: 2,073

Re: extract numbers in a text field

[ Edited ]

 

datetext="2130/3457/4732";
date1=input(scan(datetext,1,'/'),5.);
date2=input(scan(datetext,2,'/'),5.);
date3=input(scan(datetext,3,'/'),5.);

 

Contributor
Posts: 53

Re: extract numbers in a text field

Posted in reply to novinosrin

thanks so much!!

Solution
‎01-28-2018 10:41 AM
New Contributor
Posts: 2

Re: extract numbers in a text field

[ Edited ]
Posted in reply to novinosrin
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;
Esteemed Advisor
Posts: 5,626

Re: extract numbers in a text field

Note that for most analysis scenarios, you will do better in SAS with a long data structure generated by

 

datetext="2130/3457/4732";

do event = 1 to countw(datetext);
    daysToEvent = input(scan(datetext), event, "/"), best.);
    output;
    end;
drop datetext;
PG
Contributor
Posts: 53

Re: extract numbers in a text field

many thanks!
Contributor
Posts: 53

Re: extract numbers in a text field

if I may ask one more similar question

 

I have a variable "newtext" that contains a set of numbers numbers (e.g. 2,4,6,7).  For example for patient A it is stored in newtext like this "[2/4/6/7].  For a separate patient, it may contain only 2 numbers such as "[4509/21]"

  

I want to extract each of these numbers as a number variable so that for patient A:

date1=2, date2=4 date3=6 and date4=7

 

many thanks for your help

 

 

SAS Super FREQ
Posts: 9,434

Re: extract numbers in a text field

So for the second patient, the values would be: date1=4509 and date2=21? Are those really dates? I have a hard time understanding how 4509 can be a date.

cynthia
Contributor
Posts: 53

Re: extract numbers in a text field

Posted in reply to Cynthia_sas
these are number of days from a reference date (which was determined and set by the system). So for 4509 it is an interval of 12 years. The purpose of these is to encrypt the date. So I just need to extract these numbers into separate numeric fields. A given pt may have none of these numbers or 10 or 15 of these numbers so I am trying to figure out a code to extract accurately. many thanks
SAS Super FREQ
Posts: 9,434

Re: extract numbers in a text field

HI:
I agree with @PGStats -- the SCAN function will do what you want.
cynthia
Contributor
Posts: 53

Re: extract numbers in a text field

Posted in reply to Cynthia_sas

sorry, but how do I extract the numbers out of the brackets? i.e. want to get 1, 2, 3, and 4 out of "[1/2/3/4]"

 

thanks 

SAS Super FREQ
Posts: 9,434

Re: extract numbers in a text field

Hi:

  Of course, you could use TRANSLATE, TRANWRD, or COMPRESS functions to disappear away the [ ] before you start, but you can do it all with the SCAN function too. Here's an example:

data showit;
  text='[1/2/3/4]';
  x1 = scan(text,1,'[/');
  x2 = scan(text,2,'/');
  x3 = scan(text,3,'/');
  x4 = scan(text,4,'/]');
  putlog _all_;
run;

  Using an ARRAY and a DO loop just makes the program more elegant, however, I wanted to provide proof of concept here, not elegance.

 

  Hope this helps,

cynthia

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 329 views
  • 5 likes
  • 5 in conversation