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-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
  • 7 replies
  • 1067 views
  • 0 likes
  • 2 in conversation