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

Hi all,

 

I am creating a file with the codes below but there is something wrong because my first row of data is messed up. How can I fix this. Also, if I want to add to add datetime variable to this data, how can I do this.

 

data work.transfuse;

infile datalines delimiter=",";

length Type $100 Result $500;

input Encounter Type $ Result $;

datalines;

 

1240,Blood Transfusion Reason,Anemia

1240,Packed RBC Transfusion Template,Blood Transfusion Results 08/24/2018 04:21 Hemoglobin 6.9 gm/dL 08/24/2018 01:29 BG-Lactate 1.33 mmol/L 08/24/2018 07:00 Systolic BP 101 mmHg 08/24/2018 07:00 Heart Rate 95 bpm

1240,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL

1250,Blood Transfusion Reason,Anemia

1250,Packed RBC Transfusion Template,Blood Transfusion Results 08/22/2018 12:00 Systolic BP 120 mmHg 08/22/2018 12:00 Heart Rate 67 bpm

1250,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL

;

run;

proc print data=work.transfuse;

run;

 

 Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

What do you want the datetime variable to contain?

 

data work.transfuse;
infile datalines delimiter=",";
length Type $100 Result $500;
input Encounter Type $ Result $;
dt=datetime();
format dt datetime20.;
datalines;
1240,Blood Transfusion Reason,Anemia
1240,Packed RBC Transfusion Template,Blood Transfusion Results 08/24/2018 04:21 Hemoglobin 6.9 gm/dL 08/24/2018 01:29 BG-Lactate 1.33 mmol/L 08/24/2018 07:00 Systolic BP 101 mmHg 08/24/2018 07:00 Heart Rate 95 bpm
1240,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL
1250,Blood Transfusion Reason,Anemia
1250,Packed RBC Transfusion Template,Blood Transfusion Results 08/22/2018 12:00 Systolic BP 120 mmHg 08/22/2018 12:00 Heart Rate 67 bpm
1250,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL
;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

What do you want the datetime variable to contain?

 

data work.transfuse;
infile datalines delimiter=",";
length Type $100 Result $500;
input Encounter Type $ Result $;
dt=datetime();
format dt datetime20.;
datalines;
1240,Blood Transfusion Reason,Anemia
1240,Packed RBC Transfusion Template,Blood Transfusion Results 08/24/2018 04:21 Hemoglobin 6.9 gm/dL 08/24/2018 01:29 BG-Lactate 1.33 mmol/L 08/24/2018 07:00 Systolic BP 101 mmHg 08/24/2018 07:00 Heart Rate 95 bpm
1240,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL
1250,Blood Transfusion Reason,Anemia
1250,Packed RBC Transfusion Template,Blood Transfusion Results 08/22/2018 12:00 Systolic BP 120 mmHg 08/22/2018 12:00 Heart Rate 67 bpm
1250,Transfusion Indications Hemoglobin,Hemoglobin [Hgb] < 8gm/dL
;
run;
AMFR
Quartz | Level 8

I want date and time in this format 08/24/2018 08:23:00. It would be different for each encounter.

AMFR
Quartz | Level 8

How did you fix this? I cannot see any difference in your codes and my codes but your output is not messed up.

Thanks

PeterClemmensen
Tourmaline | Level 20

Some kind of magic touch I guess? 🙂

 

Joking aside, when I run your exact code I get a messed up result as well. You have a blank line as the first line inside the datalines. Usually in SAS, a blank line does not matter. However, inside the datalines is a whole other story and care must be taken. 

AMFR
Quartz | Level 8

I figured it out, there was nothing wrong with my codes. Just because of space between datalines was causing that error.

Thanks a lot

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The reason you have bad output is because your data contains the delimiter.  

1240,Blood Transfusion Reason,Anemia

        ^                                          ^

Here are two delimiters, you are reading two variables, so the third word carries over.

In a standard conformant csv file, text strings which contain the delimiter must be surrounded by quotes, e.g:

1240,"Blood Transfusion Reason,Anemia"

 

Otherwise it is not a csv file, and you will need to read the whole line in one input, then post process out the various parts later with substr or scan.  Get the file into a proper CSV file format and avoid all this trouble.

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
  • 6 replies
  • 992 views
  • 0 likes
  • 3 in conversation