DATA Step, Macro, Functions and more

reading data in data step/input

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

reading data in data step/input

Hi Everyone,

I cannot get SAS read the datalines below correctly into SAS file.

The record 4 and 5 have blank or missing value.

Can anyone help me with that?

Thank you so much.

HHC

 

data F3_final_list;
input name $32. value;

datalines;
D_VR_comgap_stb_lb6_ord3_d2_x1	100
D_W_VR_comgap_lb10_stb_cd_xac123	2 
D_std90_gt_90	0
QD_VRcomgap_stb_lb10_ord3_d1_x	
DW_VRcomgap_stb_lb6_ord3_d1_y	
D_sys2_cnt_trend_prior	0
;run;

Accepted Solutions
Solution
‎04-07-2017 10:37 AM
Super User
Posts: 9,691

Re: reading data in data step/input

data F3_final_list;
infile datalines truncover;
input name : $32. value;
datalines;
D_VR_comgap_stb_lb6_ord3_d2_x1	100
D_W_VR_comgap_lb10_stb_cd_xac123	2 
D_std90_gt_90	0
QD_VRcomgap_stb_lb10_ord3_d1_x	
DW_VRcomgap_stb_lb6_ord3_d1_y	
D_sys2_cnt_trend_prior	0
;run;

View solution in original post


All Replies
Super User
Posts: 10,548

Re: reading data in data step/input

Please note that your data lines to not have a value for the value variable.

 

QD_VRcomgap_stb_lb10_ord3_d1_x

DW_VRcomgap_stb_lb6_ord3_d1_y

there is not second value on either of these lines.

Super User
Super User
Posts: 7,430

Re: reading data in data step/input

Do you mean the dsd option:

data F3_final_list;
  infile datalines dsd;
  input name $32. value;
datalines;
D_VR_comgap_stb_lb6_ord3_d2_x1	100
D_W_VR_comgap_lb10_stb_cd_xac123	2 
D_std90_gt_90	0
QD_VRcomgap_stb_lb10_ord3_d1_x	
DW_VRcomgap_stb_lb6_ord3_d1_y	
D_sys2_cnt_trend_prior	0
;run;
Super User
Posts: 5,096

Re: reading data in data step/input

[ Edited ]

The instruction $32. is incorrect.  It tells SAS to read 32 characters, regardless of whether they are blanks or characters following blanks.  A better way:

 

length name $ 32;

input name value;

 

Also, since some lines do not contain value, this statement belongs before the INPUT statement:

 

infile datalines truncover;

Valued Guide
Posts: 505

Re: reading data in data step/input

When to use 'input; _infile_' combinations

this post
https://goo.gl/pv00gD
https://communities.sas.com/t5/forums/replypage/board-id/programming/message-id/80251

HAVE (missing data, oddly spaced data and unknown variable lengths in a SAS interactive old text editor)
=========================================================================================================

Because editors like EE, EG, SAS Studio are inconsistent with inline data, you may want stick with just one.
Line lengths, how blank lines are processed and '.' vary from editor to editor.
I like to stick with the 'old text editor', which was rock solid prior to 1984, but is deteriorating


HAVE
====

cards4;
D_VR_comgap_stb_lb6_ord3_d2_x1 100
D_W_VR_comgap_lb10_stb_cd_xac123 2
D_std90_gt_90 0
Q .
S .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior 0


WANT
====

Up to 40 obs WORK.F3_FINAL_LIST total obs=8

Obs NAME VALUE

1 D_VR_comgap_stb_lb6_ord3_d2_x1 100
2 D_W_VR_comgap_lb10_stb_cd_xac123 2
3 D_std90_gt_90 0
4 Q .
5 S .
6 QD_VRcomgap_stb_lb10_ord3_d1_x .
7 DW_VRcomgap_stb_lb6_ord3_d1_y .
8 D_sys2_cnt_trend_prior 0

WORKING CODE
============
input;
name=scan(_infile_,1,' ');
value=input(scan(_infile_,2,' '),4.);


FULL SOLUTION
=============


data F3_final_list;
length name $384 ; * longest editor line in old text editor;
input;
name=scan(_infile_,1,' ');
value=input(scan(_infile_,2,' '),4.);
cards4;
D_VR_comgap_stb_lb6_ord3_d2_x1 100
D_W_VR_comgap_lb10_stb_cd_xac123 2
D_std90_gt_90 0
Q .
S .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior 0
;;;;
run;quit;

2735 data F3_final_list;
2736 length name $384 ; * longest editor line in old text editor;
2737 input;
2738 name=scan(_infile_,1,' ');
2739 value=input(scan(_infile_,2,' '),4.);
2740 cards4;

NOTE: The data set WORK.F3_FINAL_LIST has 8 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds


In the old text editor
If you save this in your autocall library and turn on the command macros, 'options cmdmac;'


Hight light the cards data and type 'cuth' on the clean old text editor command line

%macro cuth/cmd;
%do i=1 %to 20;
c ' ' ' ' all;
%end;
%mend cuth;

You can also use a function key ie 'F1 cuth'

HIGHLIGHT THIS

D_VR_comgap_stb_lb6_ord3_d2_x1 100
D_W_VR_comgap_lb10_stb_cd_xac123 2
D_std90_gt_90 0
Q .
S .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior 0

YOU GET THIS

D_VR_comgap_stb_lb6_ord3_d2_x1 100
D_W_VR_comgap_lb10_stb_cd_xac123 2
D_std90_gt_90 0
Q .
S .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior 0


2734 run;quit;
MLOGIC(CUTH): Beginning execution.
MLOGIC(CUTH): This macro was compiled from the autocall file c:\oto\cuth.sas
MLOGIC(CUTH): %DO loop beginning; index variable I; start value is 1; stop value is 20; by value is 1.
MLOGIC(CUTH): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 4; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 5; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 6; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 7; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 8; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 9; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 10; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 11; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 12; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 13; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 14; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 15; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 16; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 17; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 18; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 19; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 20; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 21; loop will not iterate again.
MLOGIC(CUTH): Ending execution.


Valued Guide
Posts: 505

Re: reading data in data step/input

When to use 'input; _infile_' combinations

formatting not working this is a retry

see
https://goo.gl/pv00gD
https://communities.sas.com/t5/forums/replypage/board-id/programming/message-id/80251

HAVE  (missing data, oddly spaced data and unknown variable lengths in a SAS interactive old text editor)
=========================================================================================================

Because editors like EE, EG, SAS Studio are inconsistent with inline data, you may want stick with just one.
Line lengths, how blank lines are processed and '.' may vary.
I like to stick with the 'old text editor', which was rock solid prior to 1984?


HAVE
====

cards4;

D_VR_comgap_stb_lb6_ord3_d2_x1      100
D_W_VR_comgap_lb10_stb_cd_xac123                        2
D_std90_gt_90  0
Q .
S                   .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior                        0


WANT
====

Up to 40 obs WORK.F3_FINAL_LIST total obs=8

Obs    NAME                                VALUE

 1     D_VR_comgap_stb_lb6_ord3_d2_x1       100
 2     D_W_VR_comgap_lb10_stb_cd_xac123       2
 3     D_std90_gt_90                          0
 4     Q                                      .
 5     S                                      .
 6     QD_VRcomgap_stb_lb10_ord3_d1_x         .
 7     DW_VRcomgap_stb_lb6_ord3_d1_y          .
 8     D_sys2_cnt_trend_prior                 0

WORKING CODE
============
   input;
   name=scan(_infile_,1,' ');
   value=input(scan(_infile_,2,' '),4.);


FULL SOLUTION
=============


data F3_final_list;
  length name  $384 ; * longest editor line in old text editor;
  input;
  name=scan(_infile_,1,' ');
  value=input(scan(_infile_,2,' '),4.);
cards4;
D_VR_comgap_stb_lb6_ord3_d2_x1      100
D_W_VR_comgap_lb10_stb_cd_xac123                        2
D_std90_gt_90  0
Q .
S                   .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior                        0
;;;;
run;quit;

2735  data F3_final_list;
2736    length name  $384 ; * longest editor line in old text editor;
2737    input;
2738    name=scan(_infile_,1,' ');
2739    value=input(scan(_infile_,2,' '),4.);
2740  cards4;

NOTE: The data set WORK.F3_FINAL_LIST has 8 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds


In the old text editor
If you save this in your autocall library and turn on the command macros, 'options cmdmac;'


Hight light the cards data and type 'cuth' on the clean old text editor command line

%macro cuth/cmd;
  %do i=1 %to 20;
    c '  ' ' ' all;
  %end;
%mend cuth;

You can also use a function key   ie  'F1  cuth'

HIGHLIGHT THIS

D_VR_comgap_stb_lb6_ord3_d2_x1      100
D_W_VR_comgap_lb10_stb_cd_xac123                        2
D_std90_gt_90  0
Q .
S                   .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior                        0

YOU GET THIS

D_VR_comgap_stb_lb6_ord3_d2_x1 100
D_W_VR_comgap_lb10_stb_cd_xac123 2
D_std90_gt_90 0
Q .
S .
QD_VRcomgap_stb_lb10_ord3_d1_x
DW_VRcomgap_stb_lb6_ord3_d1_y
D_sys2_cnt_trend_prior 0


2734  run;quit;
MLOGIC(CUTH):  Beginning execution.
MLOGIC(CUTH):  This macro was compiled from the autocall file c:\oto\cuth.sas
MLOGIC(CUTH):  %DO loop beginning; index variable I; start value is 1; stop value is 20; by value is 1.
MLOGIC(CUTH):  %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 4; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 5; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 6; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 7; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 8; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 9; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 10; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 11; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 12; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 13; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 14; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 15; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 16; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 17; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 18; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 19; loop will iterate again.
MLOGIC(CUTH): %DO loop index variable I is now 20; loop will iterate again.
MLOGIC(CUTH):  %DO loop index variable I is now 21; loop will not iterate again.
MLOGIC(CUTH):  Ending execution.



Solution
‎04-07-2017 10:37 AM
Super User
Posts: 9,691

Re: reading data in data step/input

data F3_final_list;
infile datalines truncover;
input name : $32. value;
datalines;
D_VR_comgap_stb_lb6_ord3_d2_x1	100
D_W_VR_comgap_lb10_stb_cd_xac123	2 
D_std90_gt_90	0
QD_VRcomgap_stb_lb10_ord3_d1_x	
DW_VRcomgap_stb_lb6_ord3_d1_y	
D_sys2_cnt_trend_prior	0
;run;
Super Contributor
Posts: 371

Re: reading data in data step/input

Thank you so much.

Both codes work nicely.

HHC

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 146 views
  • 1 like
  • 6 in conversation