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

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
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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.

cj9000
Obsidian | Level 7
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. -##
Kurt_Bremser
Super User

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
cj9000
Obsidian | Level 7

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. 

Kurt_Bremser
Super User

@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.

cj9000
Obsidian | Level 7

Oh I see. Okay well thank you Kurt.

cj9000
Obsidian | Level 7

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,

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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