楼主: crackman
15197 47

跟crackman读SAS程序(15)--如何将宏变量按照字母顺序排列 [推广有奖]

41
myxixi 发表于 2010-8-25 22:47:49
是有很多种方法
不过这种方法也让我开眼界了,Thanks

42
crackman 发表于 2010-8-25 22:49:32

跟crackman读SAS程序(31)---如何动态的命名输出变量的名字

title;                                                               
/*获得数据集SASHELP.CLASS中的变量名的名字以及类型,1=数值型 2=字符型*/                                                                  
proc contents data=sashelp.class out=stuff(keep=name type) noprint;  
run;                                                                 
                                                                     
data temp;                                                           
  set stuff;                                                         
  length sumname $14.;                                               
  sumname=cats(name,'_total');   /*给每一个变量名字添加后缀_total,以示区别初始变量的名字*/                                    
run;                                                                 
                                                                     
/* look at new name */                                               
                                                                     
proc print data=temp;                                                
  title 'temp';                                                      
run;                                                                 
title;                                                               
                                                                     
/* create macro var with list of numeric variable names */           
                                                                     
proc sql;                                                            
  select (sumname)                                                   
  into :outname separated by ' '  /*将添加后缀和原始变量的名字一次赋值给宏变量*/                                   
  from temp                                                         
  where type = 1;                                                   
  select (name)                                                      
  into :inname separated by ' '                                      
  from temp                                                         
  where type = 1;                                                   
quit;                                                               
                                                                     
proc means data=sashelp.class;                                       
  var &inname;                                                      
  output out=mymeans sum= &outname;   /*计算每一个变量的汇总*/                              
run;                                                                                                                                    
proc print data=mymeans;                                             
  title 'mymeans';                                                   
run;                                                                 
      
值得学习:
1.CONTENTS使用,获得变量,变量的类型
2.SQL中的宏使用

43
crackman 发表于 2010-8-25 22:50:16
temp                 2010年08月25日 星期三 下午08时27分19秒  33

                                          Obs    NAME      TYPE      sumname

                                           1     Age         1     Age_total
                                           2     Height      1     Height_total
                                           3     Name        2     Name_total
                                           4     Sex         2     Sex_total
                                           5     Weight      1     Weight_total

                                                                                2010年08月25日 星期三 下午08时27分19秒  34

                                                      sumname
                                                      --------------
                                                      Age_total
                                                      Height_total
                                                      Weight_total

                                                                                2010年08月25日 星期三 下午08时27分19秒  35

                                             变量名称
                                             --------------------------------
                                             Age
                                             Height
                                             Weight

                                                                                2010年08月25日 星期三 下午08时27分19秒  36

                                                     MEANS PROCEDURE

               变量      标签             N            均值          标准差          最小值          最大值
               --------------------------------------------------------------------------------------------
               Age       年龄            19      13.3157895       1.4926722      11.0000000      16.0000000
               Height    身高(英寸)    19      62.3368421       5.1270752      51.3000000      72.0000000
               Weight    体重(磅)      19     100.0263158      22.7739335      50.5000000     150.0000000
               --------------------------------------------------------------------------------------------

                                                         mymeans                2010年08月25日 星期三 下午08时27分19秒  37

                                                              Age_    Height_    Weight_
                                  Obs    _TYPE_    _FREQ_    total     total      total

                                   1        0        19       253      1184.4     1900.5

44
crackman 发表于 2010-8-25 23:17:21

跟crackman读SAS程序(32)---INDSNAME获得SET数据集观测的不同来源

data one;
x=1;
run;

data two;
y=2;
run;

data new;
  set one(in=ina) two(in=inb);
  if ina then dsetname='WORK.ONE';
  else dsetname='WORK.TWO';
proc print;
run;

这个程序和下面的这个程序比较看看差别,体会一下新参数的特点
data one;
  x=1;
n=2;
run;

data two;
  y=2;
  m=3;
run;

data new;
  set one two indsname=dsn;
  dsetname=dsn;
run;
这里INDSNAME选项是为了记录读入数据的原数据集的名称,但是默认条件下INDSNAME是不会写入到输出的数据集中,所以需要设一个新的变量来保存INDSNAME的结果。

结果和第一个程序一样,不过后面的程序是9.2里面的一个新技巧

45
T星星 发表于 2010-8-26 10:08:16
请教一个问题:
程序的倒数第9行, %return;  的作用是什么?变量不符合条件,为什么要返回,返回到哪里? 谢谢!

46
T星星 发表于 2010-8-26 10:18:30
crackman 发表于 2010-8-24 10:48
%let x=100;
%let a=200;
%macro putsorted(class);                                                                                                               
                                                            
                                                        
   %let cls = %upcase(&class);                                                                                                         
   proc sql;                                                                                                                           
      create view new as select * from sashelp.vmacro                                                                                   
   %if "&cls" eq "_AUTOMATIC_" %THEN                                                                                                   
      where scope='AUTOMATIC';                                                                                                         
   %else %if "&cls" eq "_GLOBAL_" %then                                                                                                
      where scope='GLOBAL';                                                                                                            
   %else %if "&cls" eq "_LOCAL_" %then                                                                                                  
      where scope^='AUTOMATIC' and scope^='GLOBAL' and scope^='PUTSORTED';                                                              
   %else %if "&cls" eq "_USER_" %then                                                                                                   
      where scope^='AUTOMATIC' and scope^='PUTSORTED';                                                                                 
   %else %if "&cls" eq "_ALL_" %then                                                                                                   
      where scope^='PUTSORTED';                                                                                                         
   %else %do;                                                                                                                           
      %put Unrecognized CLASS of macro variables: &cls;                                                                                 
      %return;                                                                                                                          
   %end;                                                                                                                                
      order by name;                                                                                                                    
   quit;                                                                                                                                
   data _null_;                                                                                                                        
      set new;                                                                                                                          
      put scope +1 name +1 value;                                                                                                      
   run;                                                                                                                                 
%mend;                                                                                                                                 
                                                                                                                                       
%putsorted(_global_)
其实这个里面值得学习的一点就是在宏里面用SQL根据IF  THEN ELSE判断不同的筛选条件来获得不同作用范围的宏变量按照字母顺序输出
请教一个问题:
程序的倒数第9行, %return;  的作用是什么?变量不符合条件,为什么要返回,返回到哪里? 谢谢!

47
crackman 发表于 2010-8-26 11:30:25
%return
主要是用来终止当前正在执行的宏程序
The %RETURN macro causes normal termination of the currently executing macro.

48
梦想世界1 发表于 2013-12-3 16:24:19
斑竹好无私,赞一个

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-25 05:30