Help using Base SAS procedures

input problem

Reply
Frequent Contributor
Posts: 89

input problem

suppose I have following data:


blank 1 1 1
2 blank 2 2
3 3 blank 3
4 4 4 blank


The delimiter is a blank (" "). And the missing value is also denoted as a blank(i.e. " ". I type "blank" in the data just to clarify).

what input code can work for such data?
thanks.
Super User
Posts: 10,035

Re: input problem

Posted in reply to littlestone
Hi.
That column input method would be used.
[pre]
data temp;
input a 1 b 3 c 5 d 7;
cards;
1 1 1
2 2 2
3 3 3
4 4 4
;run;
[/pre]




Ksharp
Frequent Contributor
Posts: 89

Re: input problem

Thank you for reply. However, what if raw data look like this:

blank 1 1 1
2 blank 22 2
3 333 blank 3
4444 4 4 blank
Respected Advisor
Posts: 4,173

Re: input problem

Posted in reply to littlestone
Something like below could work:

data test;
infile datalines dsd delimiter='|' truncover;
input @;
_infile_=tranwrd(trim(_infile_),' ','|');
_infile_=tranwrd(trim(_infile_),' ','|');
input a b c d;
put a= b= c= d=;
datalines;
blank 1 1 1
2 blank blank 2
3 333 blank 3
4444 4 4 blank
;
run;

...or even so (the left over blanks have now only the meaning of "delimiter"):

data test;
infile datalines dsd delimiter='| ' truncover;
input @;
_infile_=tranwrd(trim(_infile_),' ','|');
input a b c d;
put a= b= c= d=;
datalines;
blank 1 1 1
2 blank blank 2
3 333 blank 3
4444 4 4 blank
;
run;

HTH
Patrick Message was edited by: Patrick
PROC Star
Posts: 7,474

Re: input problem

I would try:


data test;
infile datalines dsd delimiter='|' truncover;
length x $40;
input @;
_infile_=tranwrd(_infile_,' ','|');
_infile_=tranwrd(_infile_,' ','|');
input a b c d;
datalines;
1 1 1
2 2
3 333 3
4444 4 4
;
run;

HTH,
Art
Valued Guide
Posts: 2,177

Re: input problem

the only problem seems to be that a blank item has space for itself on the input line as well as the delimiter and DSD infile option considers consecutive delimiters imply an empty item. So just reduce two blanks to one. This is very similar to earlier posts, but uses only one tranwrd().[pre]data test ;
infile datalines dsd delimiter=' ' truncover;
input @ ;
_infile_= tranwrd( _infile_,' ',' ' );
input a b c d;
put (_all_)(=) ;
datalines;
1 1 1
2 2
3 333 3
4444 4 4
;[/pre]* and here is a log of the step extended to demo the tranwrd() effect on _infile_;[pre]857 data demo ;
858 infile datalines dsd delimiter=' ' truncover;
859 input @ ;
860 put _infile_ $char20. +1 'before the fix' ;
861 _infile_= tranwrd( _infile_,' ',' ' );
862 put _infile_ $char20. +1 'after' ;
863 input a b c d;
864 put (_all_)(=) ;
865 datalines;

1 1 1 before the fix
1 1 1 after
a=. b=1 c=1 d=1
2 2 before the fix
2 2 after
a=2 b=. c=. d=2
3 333 3 before the fix
3 333 3 after
a=3 b=333 c=. d=3
4444 4 4 before the fix
4444 4 4 after
a=4444 b=4 c=4 d=.
NOTE: The data set WORK.DEMO has 4 observations and 4 variables.
NOTE: DATA statement
[/pre]
peterC
Frequent Contributor
Posts: 89

Re: input problem

Posted in reply to littlestone
Thank you all for help.
I apologize for not replying soon enough. I will try all your suggestions and get back to you.
Again, thank you very much.
Frequent Contributor
Posts: 89

Re: input problem

Posted in reply to littlestone
i tried all codes and Mr. Peter.C's code worked best for me.

Again thank you all for help. I really learned a lot.
Super User
Posts: 10,035

Re: input problem

Posted in reply to littlestone
Hi.Actuall, Patrick and art297 's code is also right.
If they put [ pre ] before their code and [ /pre ] after their code,that would not trim the blanks in code.

Ksharp

Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 8 replies
  • 219 views
  • 0 likes
  • 5 in conversation