BookmarkSubscribeRSS Feed

[SAS 프로그래밍 고수 백승민] [INFILE] 불규칙 데이터 읽기(_INFILE_ 사용)

Started ‎06-11-2020 by
Modified ‎06-11-2020 by
Views 168

[ 질문 : http://cafe.daum.net/statsas/B3m/13030 ]

다음과 같은 자료를 읽어들일때 어떤식으로 코딩을 해야하나요?

 

참고로 변수명은 rank, country, rate, date 입니다.

 

datalines;

1 United Arab Emirates 2.75 2010 est.  
2 Qatar 2.44 2010 est. 
3 United Arab Emirates 2.20 2010 est. 
4 Montserrat 2.03 2010 est. 
5 Qatar 1.99 2010 est. 
6 United Arab Emirates 1.80 2010 est. 
7 Kuwait 1.79 2010 est. 

;

 

[최홍규 님 답변]

/*

call scan 에 대한 도움말에 지금 문제를 해결할 수 있는 좋은 예제가 있어서 응용해 봤습니다.

*/

 

data call_scan_sample;

   array v(99) $100.;
   informat rank best. country $30. rate date best. est $30.;

 

   input @;

 

   do i=1 to 99;
      call scanq(_infile_, i, position, length);
      if not position then leave;
      v(i)=substrn(_infile_, position, length);
   end;

 

   input;

 

   rank=input(v(1),best.);
   do j=2 to i-4;
     country=trim(country)||' '||v(j);
   end;
   rate=input(v(i-3),best.);
   date=input(v(i-2),best.);
   est=v(i-1);

 

keep rank country rate date est;
 

datalines;
1 United Arab Emirates 2.75 2010 est. 
2 Qatar 2.44 2010 est. 
3 United Arab Emirates 2.20 2010 est. 
4 Montserrat 2.03 2010 est. 
5 Qatar 1.99 2010 est. 
6 United Arab Emirates 1.80 2010 est. 
7 Kuwait 1.79 2010 est. 
;
run;

proc print;run;

 

[ 백승민 답변]

 

* 최홍규님께서 올려주신 답글을 참고로 구현해 보았습니다.

* 확장성은 떨어지지만. _infile_ 함수를 이용 방법이 재미있네요.;

* 감사합니다.;

 

data BACK;
     LENGTH VAR1 $1. VAR2 $50. VAR3 $4. VAR4 $4. VAR5 $4.;
   input ;
   var1 = SUBSTR(_infile_,1,1);
   var2 = SUBSTR(_INFILE_,3,ANYDIGIT(_INFILE_,2)-3);
   var3 = SCANQ(_INFILE_,-3);
   var4 = SCANQ(_INFILE_,-2);
   var5 = SCANQ(_INFILE_,-1);
datalines;
1 United Arab Emirates 2.75 2010 est. 
2 Qatar 2.44 2010 est. 
3 United Arab Emirates 2.20 2010 est. 
4 Montserrat 2.03 2010 est. 
5 Qatar 1.99 2010 est. 
6 United Arab Emirates 1.80 2010 est. 
7 Kuwait 1.79 2010 est. 
;
run;

Version history
Last update:
‎06-11-2020 05:12 AM
Updated by:
Contributors

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Article Labels
Article Tags