DATA Step, Macro, Functions and more

reading in more data

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

reading in more data

HI again.

I added another column and data and formatted the input statement as instructed. It seems to have blown up though. Here's the code: Thanks in advance:

 

data widepe ;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c: & $24. peheent_c: & $24.;
datalines;
0100-1204 screening Abnormal, please specify Abnormal, please specify
0100-1204 eos/et Normal normal
0100-1205 Screening Normal normal
0100-1205 EOS/ET Normal Abnormal, please specify
;
run;
proc print data = widepe; run;

 

SAS Output

Obs subjectnumberstr visitmnemonic pegenap_c peheent_c
1 0100-1204 screening Abnormal, please specify 0100-1204 eos/et Normal
2 0100-1205 Screening Normal normal 0100-1205 EOS/ET Normal

Accepted Solutions
Solution
‎05-05-2017 07:37 AM
Super User
Posts: 6,936

Re: reading in more data

Oh, totally missed the blanks in the data.

 

To read variable length strings that contain blanks, you need either a delimiter in the input data, or you have to enclose the strings in quotes. Then you can use the dsd option:

data widepe;
infile cards dlm="|" truncover;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c:  $24. peheent_c:  $24.;
datalines;
0100-1204|screening|Abnormal, please specify|Abnormal, please specify
0100-1204|eos/et|Normal|normal
0100-1205|Screening|Normal|normal
0100-1205|EOS/ET|Normal|Abnormal, please specify
;
run;

proc print data=widepe noobs;
run;

data widepe;
infile cards dlm=" " dsd truncover;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c:  $24. peheent_c:  $24.;
datalines;
0100-1204 "screening" "Abnormal, please specify" "Abnormal, please specify"
0100-1204 "eos/et" "Normal" "normal"
0100-1205 "Screening" "Normal" "normal"
0100-1205 "EOS/ET" "Normal" "Abnormal, please specify"
;
run;

proc print data=widepe noobs;
run;

Results:

subjectnumberstr    visitmnemonic    pegenap_c                   peheent_c

   0100-1204          screening      Abnormal, please specify    Abnormal, please specify
   0100-1204          eos/et         Normal                      normal                  
   0100-1205          Screening      Normal                      normal                  
   0100-1205          EOS/ET         Normal                      Abnormal, please specify

subjectnumberstr    visitmnemonic    pegenap_c                   peheent_c

   0100-1204          screening      Abnormal, please specify    Abnormal, please specify
   0100-1204          eos/et         Normal                      normal                  
   0100-1205          Screening      Normal                      normal                  
   0100-1205          EOS/ET         Normal                      Abnormal, please specify
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,936

Re: reading in more data

Remove the ampersands.

data widepe ;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c:  $24. peheent_c:  $24.;
datalines;
0100-1204 screening Abnormal, please specify Abnormal, please specify
0100-1204 eos/et Normal normal
0100-1205 Screening Normal normal
0100-1205 EOS/ET Normal Abnormal, please specify
;
run;
proc print data = widepe; run;

works.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 27

Re: reading in more data

Hmmm Thanks Kurt but when i ran the code I got:

Obs subjectnumberstr visitmnemonic pegenap_c peheent_c
1 0100-1204 screening Abnormal, please
2 0100-1204 eos/et Normal normal
3 0100-1205 Screening Normal normal
4 0100-1205 EOS/ET Normal Abnormal,
So then i tried increasing the lengths of the last two variables but got
the same results


##- Please type your reply above this line. Simple formatting, no
attachments. -##
Solution
‎05-05-2017 07:37 AM
Super User
Posts: 6,936

Re: reading in more data

Oh, totally missed the blanks in the data.

 

To read variable length strings that contain blanks, you need either a delimiter in the input data, or you have to enclose the strings in quotes. Then you can use the dsd option:

data widepe;
infile cards dlm="|" truncover;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c:  $24. peheent_c:  $24.;
datalines;
0100-1204|screening|Abnormal, please specify|Abnormal, please specify
0100-1204|eos/et|Normal|normal
0100-1205|Screening|Normal|normal
0100-1205|EOS/ET|Normal|Abnormal, please specify
;
run;

proc print data=widepe noobs;
run;

data widepe;
infile cards dlm=" " dsd truncover;
input subjectnumberstr: $9. visitmnemonic: $9. pegenap_c:  $24. peheent_c:  $24.;
datalines;
0100-1204 "screening" "Abnormal, please specify" "Abnormal, please specify"
0100-1204 "eos/et" "Normal" "normal"
0100-1205 "Screening" "Normal" "normal"
0100-1205 "EOS/ET" "Normal" "Abnormal, please specify"
;
run;

proc print data=widepe noobs;
run;

Results:

subjectnumberstr    visitmnemonic    pegenap_c                   peheent_c

   0100-1204          screening      Abnormal, please specify    Abnormal, please specify
   0100-1204          eos/et         Normal                      normal                  
   0100-1205          Screening      Normal                      normal                  
   0100-1205          EOS/ET         Normal                      Abnormal, please specify

subjectnumberstr    visitmnemonic    pegenap_c                   peheent_c

   0100-1204          screening      Abnormal, please specify    Abnormal, please specify
   0100-1204          eos/et         Normal                      normal                  
   0100-1205          Screening      Normal                      normal                  
   0100-1205          EOS/ET         Normal                      Abnormal, please specify
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 27

Re: reading in more data

Thank you Kurt for the solution, it works great. But I thought the ampersand modifier allowed for sas to read blanks within the variable length but I guess not. 

Super User
Posts: 6,936

Re: reading in more data


cj9000 wrote:

Thank you Kurt for the solution, it works great. But I thought the ampersand modifier allowed for sas to read blanks within the variable length but I guess not. 


You are referring to this:

 

&

indicates that a character value can have one or more single embedded blanks. This format modifier reads the value from the next non-blank column until the pointer reaches two consecutive blanks, the defined length of the variable, or the end of the input line, whichever comes first.

 

(underline by me)

 

For that to work, you would have needed two blanks in succession as delimiters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 27

Re: reading in more data

Oh I see. Okay well thank you Kurt.

Contributor
Posts: 27

Re: reading in more data

This might display better, this is what I got.

 

SAS Output

Obs subjectnumberstr visitmnemonic pegenap_c peheent_c
1 0100-1204 screening Abnormal, please
2 0100-1204 eos/et Normal normal
3 0100-1205 Screening Normal normal
4 0100-1205 EOS/ET Normal Abnormal,
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 116 views
  • 0 likes
  • 2 in conversation