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
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;
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;
I want date and time in this format 08/24/2018 08:23:00. It would be different for each encounter.
How did you fix this? I cannot see any difference in your codes and my codes but your output is not messed up.
Thanks
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.
I figured it out, there was nothing wrong with my codes. Just because of space between datalines was causing that error.
Thanks a lot
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.