简体   繁体   English

SAS宏数据线SAS

[英]sas macro datalines sas

data macpro.bimodels_type;
infile datalines ;
input Model & $ model_class$ model_price model_frame$ DOpurchase;
*length Model$20. model_class$25. model_price4. ;
datalines;
Black Bora    Track        796   Aluminum  01Aug2009 
Delta Breeze  Road        400   CroMoly     23Aug2010 
Jet Stream      Track       1160  CroMoly    01Aug2009 
Mistral            Road        1995 Carbon Comp 01Jul2010 
Nor'easter      Mountain  900 Aluminum        05Jul2010 
Santa Ana      Mountain  459 Aluminum        20Jul2010 
Scirocco         Mountain  2300 Titanium        08Sep2011 
Trade Wind    Road          759 Aluminum       08Sep2011 

in Model variable i where trying to fix blank,​ i used & syntax like input Model & $ and so on... but i can't able to fix Black Bora in single column. Model变量i中尝试修复空白的地方,我使用了&语法,例如input Model & $等等...但是我无法在单列中修复Black Bora how i can fix this. 我该如何解决这个问题。

You cannot change the length of a character variable after SAS has already determined what it will be. 在SAS已经确定字符变量的长度之后,您将无法更改它的长度。 I find it much easier to get it right if I explicitly define the variables before using them in other statements. 如果在其他语句中使用变量之前显式定义变量,则可以更轻松地实现此目的。

data bimodels_type;
   infile datalines truncover ;
   length Model $20 model_class $25 model_price 8 model_frame $20 DOpurchase 8 ;
   format DOpurchase date9. ;
   input Model & model_class model_price model_frame & DOpurchase :date.;
datalines;
Black Bora    Track        796   Aluminum  01Aug2009 
Delta Breeze  Road        400   CroMoly     23Aug2010 
Jet Stream      Track       1160  CroMoly    01Aug2009 
Mistral            Road        1995 Carbon Comp 01Jul2010 
Nor'easter      Mountain  900 Aluminum        05Jul2010 
Santa Ana      Mountain  459 Aluminum        20Jul2010 
Scirocco         Mountain  2300 Titanium        08Sep2011 
Trade Wind    Road          759 Aluminum       08Sep2011 
;

For the & modifier to work you need to have at least two spaces (delimiters actually) after the value and before the next value. 为了使&修饰符起作用,您需要在值之后和下一个值之前至少有两个空格(实际上是定界符)。 So in your data the fourth line is going to have a problem since there are not two spaces before the date. 因此,在数据中第四行将出现问题,因为日期之前没有两个空格。

You could either fix the input data. 您可以修复输入数据。

Mistral            Road        1995 Carbon Comp  01Jul2010 

Or if you know that every record will have a date you could read it as part of the previous character variable and then take it back out. 或者,如果您知道每条记录都会有一个日期,则可以将其读取为先前字符变量的一部分,然后将其取出。

data bimodels_type;
   infile datalines truncover ;
   length Model $20 model_class $25 model_price 8 model_frame $50 DOpurchase 8 ;
   format DOpurchase date9. ;
   input Model & model_class model_price model_frame $50.;
   DOpurchase=input(scan(model_frame,-1,' '),date11.);
   model_frame = substr(model_frame,1,length(model_frame)-9);
datalines;
Black Bora    Track        796   Aluminum  01Aug2009
Delta Breeze  Road        400   CroMoly     23Aug2010
Jet Stream      Track       1160  CroMoly    01Aug2009
Mistral            Road        1995 Carbon Comp 01Jul2010
Nor'easter      Mountain  900 Aluminum        05Jul2010
Santa Ana      Mountain  459 Aluminum        20Jul2010
Scirocco         Mountain  2300 Titanium        08Sep2011
Trade Wind    Road          759 Aluminum       08Sep2011
;

If you are indeed trying to wrap your code containing datalines inside a macro (as the title suggests), this won't work. 如果确实要在宏中包装包含数据行的代码(如标题所示),则此方法将无效。 Datalines/cards are statements that cannot be executed within a macro. 数据线/卡是不能在宏内执行的语句。 As an alternative, you may save your datalines in a text file and read in that file, which works within macro code. 或者,您可以将数据行保存在一个文本文件中,然后读取该文件,该文件在宏代码中有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM