2014-04-06 1 views
1

나는 다음과 같은 데이터 파일 I 특정 주소에 살고있는 사람들의 수를 계산하려고SAS에서 헤더 레코드 당 하나의 관찰을 읽는 방법은 무엇입니까?

H 321 s main st 
P mary e  21 f 
P aby e  23 m 
P stary e  31 f 
P dory e  23 m 
H 321 s second st 
P lary e  31 m 
P laby e  43 m 
P ltary e  31 m 
P lory e  23 m 
P lwey e  43 f 
P lwty e  35 f 
P lowtetr e 25 m 
H 4351 s 35343nd st 

있습니다. 결과 데이터 세트에는 3 회의 관측치가 있어야합니다.

여기에 코드를 내가 하나 개의 행을 얻을 그러나

data ch21.test2 ; 
    infile testFile end = last ; 
    retain address ; 

    input type $1. @ ; 
      if type = 'H' then 
       do ; 
       if _n_ > 1 then 
       output ; 
       total=0 ; 
       input @3 address $3-21 ;   
       end ; 
      else if type='P' then 
       input @3 name $12. +1 age 2. +1 gender $1. ; 
       total+1 ; 
       if last then 
       output ; 
    run ; 

입니다.

답변

-2

&가 누락되었습니다. 데이터에 공백이 있습니다. 이것을 시도하십시오

data ch21.test2 ; 
    infile testFile end = last ; 
    retain address ; 

    input type $1. @ ; 
      if type = 'H' then 
       do ; 
       if _n_ > 1 then 
       output ; 
       total=0 ; 
       input @3 address & $18. ;  /* ADDED & and corrected to $18. */ 
       end ; 
      else if type='P' then 
       input @3 name & $12. +1 age 2. +1 gender $1. ; /* ADDED & */ 
       total+1 ; 
       if last then 
       output ; 
    run ; 
+0

올바르지 않습니다. 그는 목록 입력을 사용하려고하지 않습니다. 특히 형식화 된 입력으로 올바르게 변환하면'&'는 더 이상 아무것도하지 않습니다. 위의 코드에는 몇 가지 문제가 있으며이 문제는 해결되지 않았습니다. – Joe

2

왜 코드에서 한 행 밖에 없습니까? 내가 실행할 때 두 개가 생깁니다 (아마도 세 개입니다. datalinesend 변수를 지원하지 않습니다). 그러나 else 조건 주위에 do 루프가 없으므로 잘못된 답을 얻게됩니다 (아마도 하나보다 높을 수 있습니다). 귀하의 의견은 입력 스타일을 결합하기 때문에 조금 혼란 스럽지만 특별히 잘못한 것은 아닙니다. 나는 그것을 아래로 바꾸어서 내가 편하게 생각하지만 너의 작품도 잘 읽는다. 그러나 나는 좋은 아이디어 인 type에 @ 1을 추가했다. 예기치 않은 입력 문제가있는 경우 @ 1은 첫 번째 문자를 읽고 있는지 확인합니다 (원하는대로).

한 행만 표시되는 경우 데이터 파일 형식에 문제가있을 수 있습니다. 아마도 유닉스 파일이고 윈도우 머신에서 읽고 있기 때문에 EOL 문자를 존중하지 않는다.

data test2 ; 
infile datalines end = last ; 
retain address ; 

input @1 type $1. @ ; *add @1; 
     if type = 'H' then 
     do ; 
      if _n_ > 1 then 
      output ; 
      total=0 ; 
      input @3 address $19. ; *converted to formatted style; 
     end ; 
     else if type='P' then do; *added do - you had indented here but did not have a do; 
      input @3 name $12. @15 age 2. @18 gender $1.; *converted to all formatted style; 
      total+1 ; 
     end;      *added end - assuming if last then output should be outside?; 
     if last then 
      output ; 
datalines; 
H 321 s main st 
P mary e  21 f 
P aby e  23 m 
P stary e  31 f 
P dory e  23 m 
H 321 s second st 
P lary e  31 m 
P laby e  43 m 
P ltary e  31 m 
P lory e  23 m 
P lwey e  43 f 
P lwty e  35 f 
P lowtetr e 25 m 
H 4351 s 35343nd st 
;;;; 
run; 
관련 문제