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
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;
datetext="2130/3457/4732";
date1=input(scan(datetext,1,'/'),5.);
date2=input(scan(datetext,2,'/'),5.);
date3=input(scan(datetext,3,'/'),5.);
thanks so much!!
data q1;
infile datalines dlm="/";
input date1 date2 date3;
datalines;
2130/3457/4732
run;
proc print data=q1;
run;
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;
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
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
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
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.
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.