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

 

Hi , Below code is working fine when we have two spaces before Score column values.

data example1;
input ID Name & $30. Score;
cards;
1 Deepanshu Bhalla  22
2 Atta Pat  21
3 Xonxiangnam Samnuelnarayan  33
;

When we have single space before score column value , it's not working properly.

data example1;
input ID Name & $30. Score;
cards;
1 Deepanshu Bhalla 22
2 Atta Pat 21
3 Xonxiangnam Samnuelnarayan 33
;

 

Any suggestion to resolve this ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data want;
infile cards truncover;
input id temp $80.;
call scan(temp,-1,p,l,' ');
name=substr(temp,1,p-1);
score=scan(temp,-1,' ');
drop temp p l;
cards;
1 Deepanshu Bhalla 22
2 Atta Pat 21
3 Xonxiangnam Samnuelnarayan 33
4 first middle last 55
;


proc print;run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Maxim 1: Read the Documentation.

From the documentation of list input:

&

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.

 

(emphasis by me)

AshokD
Obsidian | Level 7

@Kurt_Bremser

 

Thanks for the link.

Any other input technique to read the below content ? I need to get the first name and last name in one column while reading the data.

 

cards;
1 Deepanshu Bhalla 22
2 Atta Pat 21
3 Xonxiangnam Samnuelnarayan 33
;

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not without coding it yourself.  If you have data which is delimited, then the delimiter must not appear in the data, otherwise it is invalid, simple fact.  Return this broken data to be fixed at source, or spend your life trying to fix it:

data want;
  length line $200;
  infile "...";
  input line $;
  id=input(scan(line,1," "),best.);
  line=scan(line,2," ");
  age=input(substr(line,lengthn(line)-2,2),best.);
  name=tranwrd(line,strip(put(age,best.)),"");
run;

Not tested, just to show you how much of mess having bad data is.

Kurt_Bremser
Super User

You can use the automatic variable _infile_ to dissect the input line on your own:

data want;
length
  id $2
  name $30
  score 8
;
input;
id = scan(_infile_,1);
score = input(scan(_infile_,-1),best.);
do i = 2 to countw(_infile_) - 1;
  name = catx(' ',name,scan(_infile_,i));
end;
drop i;
cards;
1 Deepanshu Bhalla 22
2 Atta Pat 21
3 Xonxiangnam Samnuelnarayan 33
4 first middle last 55
;

But frankly, this has to be dealt with through the data format itself:

  • fixed-width columns or
  • a delimiter that is not present in the data items (eg the pipe character |)
  • or by enclosing data items in quotes (and using the dsd option in SAS)
Ksharp
Super User
data want;
infile cards truncover;
input id temp $80.;
call scan(temp,-1,p,l,' ');
name=substr(temp,1,p-1);
score=scan(temp,-1,' ');
drop temp p l;
cards;
1 Deepanshu Bhalla 22
2 Atta Pat 21
3 Xonxiangnam Samnuelnarayan 33
4 first middle last 55
;


proc print;run;
AshokD
Obsidian | Level 7

@Ksharp 

Thank you so much for the suggestion.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 797 views
  • 0 likes
  • 4 in conversation