BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rykwong
Quartz | Level 8

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Vipul_Sawlani
Calcite | Level 5
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;

View solution in original post

11 REPLIES 11
novinosrin
Tourmaline | Level 20

 

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

 

rykwong
Quartz | Level 8

thanks so much!!

Vipul_Sawlani
Calcite | Level 5
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;
PGStats
Opal | Level 21

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
rykwong
Quartz | Level 8
many thanks!
rykwong
Quartz | Level 8

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

 

 

Cynthia_sas
SAS Super FREQ
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
rykwong
Quartz | Level 8
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
Cynthia_sas
SAS Super FREQ
HI:
I agree with @PGStats -- the SCAN function will do what you want.
cynthia
rykwong
Quartz | Level 8

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 

Cynthia_sas
SAS Super FREQ

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 11 replies
  • 1326 views
  • 5 likes
  • 5 in conversation