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

I have a a dataset where the first row is concatenated, delimited by '|'. I am using the following code to unconcatenate it. This works fine but then I want to drop patient_id which ends up deleting some patient rows. I suspect this has to do with the 'uniqueness' of the row but I am not sure. How can I unconcatenate, drop patient_id and keep all the rows?

 

data test;
  set have;
  patient=scan(patient_id, 1, '|');
  variable=scan(patient_id, 2, '|');
  date=scan(patient_id, 3, '|');
  visit=scan(patient_id, 4, '|');
run;
have:
patiend_id                   Ht Wt Bm
DH-4-1|H|28SEP2017:00:00:00  55 55 55
DH-4-1|W|22SEP2017:00:00:00|A 55 55 55
DH-4-1|H|22SEP2017:00:00:00|B 55 55 55
want:
patiend_id variable         Date       Visit    Ht Wt Bm
DH-4-1       H      28SEP2017:00:00:00          55 55 55
DH-4-1       W      22SEP2017:00:00:00   A      55 55 55
DH-4-1       H      22SEP2017:00:00:00   B      55 55 55
what I'm getting if I drop patient_id: 
patiend_id variable         Date       Visit    Ht Wt Bm
DH-4-1       W      22SEP2017:00:00:00   A      55 55 55

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

So how are you "dropping patient_id"? You don't show any code. Your result:

what I'm getting if I drop patient_id: 
patiend_id variable         Date       Visit    Ht Wt Bm
DH-4-1       W      22SEP2017:00:00:00   A      55 55 55

looks like you have done something to remove PATIENT, not patien_id

 

 

It sounds like you may be using Delete, which removes records, not variables.

 

If you are attempting to conditionally remove patient_id, then than isn't quite possible. A variable is either in or not in a data set. The statement would be

 

data test;
  set have;
  patient=scan(patient_id, 1, '|');
  variable=scan(patient_id, 2, '|');
  date=scan(patient_id, 3, '|');
  visit=scan(patient_id, 4, '|');

  drop patient_id;
run;

 

which means that the output data set would never have the variable patient_id but keeps patient, variable, date, visit;

 

Note that you would be better not to call the value "date" as it has a time component and SAS makes a big difference between date and datetime values.

Also, read and treat as a datetime:

Instead of

date=scan(patient_id, 3, '|');

try

datetime = input(scan(patient_id,3,'|'),datetime.);

format datetime datetime.;

OR if all of your times are 00:00:00 because of some other sources slopping program and you actually want a DATE then

 

datetime = input(scan(patient_id,3,'|'),date9.);

format datetime date9.;

 

There are many tools in SAS to work with actual date, time or datetime values and likely anything you want to with that value will require creating a date or datetime value first, might as well do it first thing.

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User
Is this from a text file? How did you originally read it into SAS?
ballardw
Super User

So how are you "dropping patient_id"? You don't show any code. Your result:

what I'm getting if I drop patient_id: 
patiend_id variable         Date       Visit    Ht Wt Bm
DH-4-1       W      22SEP2017:00:00:00   A      55 55 55

looks like you have done something to remove PATIENT, not patien_id

 

 

It sounds like you may be using Delete, which removes records, not variables.

 

If you are attempting to conditionally remove patient_id, then than isn't quite possible. A variable is either in or not in a data set. The statement would be

 

data test;
  set have;
  patient=scan(patient_id, 1, '|');
  variable=scan(patient_id, 2, '|');
  date=scan(patient_id, 3, '|');
  visit=scan(patient_id, 4, '|');

  drop patient_id;
run;

 

which means that the output data set would never have the variable patient_id but keeps patient, variable, date, visit;

 

Note that you would be better not to call the value "date" as it has a time component and SAS makes a big difference between date and datetime values.

Also, read and treat as a datetime:

Instead of

date=scan(patient_id, 3, '|');

try

datetime = input(scan(patient_id,3,'|'),datetime.);

format datetime datetime.;

OR if all of your times are 00:00:00 because of some other sources slopping program and you actually want a DATE then

 

datetime = input(scan(patient_id,3,'|'),date9.);

format datetime date9.;

 

There are many tools in SAS to work with actual date, time or datetime values and likely anything you want to with that value will require creating a date or datetime value first, might as well do it first thing.

 

 

Ksharp
Super User

Try add blank into split char and use 'M' option.

 

data test;
  set have;
  patient=scan(patient_id, 1, '| ','m');
  variable=scan(patient_id, 2, '| ','m');
  date=scan(patient_id, 3, '| ','m');
  visit=scan(patient_id, 4, '| ','m');
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 736 views
  • 2 likes
  • 4 in conversation