楼主: shewenhao
4057 10

[原创博文] 请问sas输出data的命名问题 [推广有奖]

  • 1关注
  • 7粉丝

已卖:1586份资源

讲师

42%

还不是VIP/贵宾

-

威望
0
论坛币
40029 个
通用积分
14.0612
学术水平
4 点
热心指数
6 点
信用等级
3 点
经验
563 点
帖子
235
精华
0
在线时间
617 小时
注册时间
2008-3-7
最后登录
2025-1-16

楼主
shewenhao 发表于 2010-2-3 17:35:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
先用sas的import命令读入一个文件,文件是excel,比如B2是这个文件的编号,其他部分是data,我如何能让这个B2的内容作为这个data的名字呢?

命令如下

import datafile = 'file-name' out = histdata(存储位置).data-set(这个data set我就想用B2的内容自动添加)

各位高人,我该如何做呢?
先谢谢拉!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Data import EXCEL Port file import excel 命名 如何

回帖推荐

hongxx 发表于4楼  查看完整内容

import过程内部实际是用infile语句来弄的,而infile有个选项filename=variable,可以定义一个变量variable记录你打开的文件物理路径,像last之类一样,该变量没有进入数据集。你可以作为在data部用宏symput输出到宏变量, txt文本如下: "Indexcd","Trddt","Daywk","Retindex" "000001","1995-01-03","2","-0.012333" "000001","1995-01-04","3","0.021770" "000001","1995-01-05","4","-0.010584" "000001","1995-01-06", ...

本帖被以下文库推荐

沙发
醉_清风 发表于 2010-2-3 18:07:56
搞个宏试试看吧
从来不需要想起 永远也不会忘记

藤椅
shewenhao 发表于 2010-2-3 18:23:49
谢谢,正在尝试sql和macro

板凳
hongxx 发表于 2010-2-3 21:18:14
import过程内部实际是用infile语句来弄的,而infile有个选项filename=variable,可以定义一个变量variable记录你打开的文件物理路径,像last之类一样,该变量没有进入数据集。你可以作为在data部用宏symput输出到宏变量,
txt文本如下:

"Indexcd","Trddt","Daywk","Retindex"
"000001","1995-01-03","2","-0.012333"
"000001","1995-01-04","3","0.021770"
"000001","1995-01-05","4","-0.010584"
"000001","1995-01-06","5","-0.009476"
"000001","1995-01-09","1","-0.023035"
"000001","1995-01-10","2","-0.025080"

data;
   length fname $300;   
   infile  'D:\实验四\dayret.txt' delimiter=',' missover dsd firstobs=2 obs=4 filename=fname;   
   input indexcd :$8. date :$10. weekday :$1. indexret :best12.;
   temp=index(fname,'.txt');
   call symput('name',substr(fname,11,temp-11));
run;
data &name;
  set _last_;
run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

报纸
crackman 发表于 2010-2-3 22:23:21
学些到了
谢谢

地板
shewenhao 发表于 2010-2-3 22:26:29
四楼太牛了,向你学习!

7
hongxx 发表于 2010-2-9 10:38:59
类似的如:
(以下可见sas help 的sql)
Creating and Using Macro Variables

--------------------------------------------------------------------------------

Problem


You want to create a separate data set for each unique value of a column.


--------------------------------------------------------------------------------

Background Information


The SQL.FEATURES data set contains information on various geographical features around the world.


FEATURES (Partial Output)

                                      FEATURES

Name             Type        Location             Area    Height     Depth    Length
------------------------------------------------------------------------------------
Aconcagua        Mountain    Argentina               .     22834         .         .
Amazon           River       South America           .         .         .      4000
Amur             River       Asia                    .         .         .      2700
Andaman          Sea                            218100         .      3667         .
Angel Falls      Waterfall   Venezuela               .      3212         .         .
Annapurna        Mountain    Nepal                   .     26504         .         .
Aral Sea         Lake        Asia                25300         .       222         .
Ararat           Mountain    Turkey                  .     16804         .         .
Arctic           Ocean                         5105700         .     17880         .
Atlantic         Ocean                        33420000         .     28374         .


--------------------------------------------------------------------------------

Solution


To create a separate data set for each type of feature, you could go through the data set manually to determine all the unique values of Type, and then write a separate DATA step for each type (or a single DATA step with multiple OUTPUT statements). This approach is labor-intensive, error-prone, and impractical for large data sets. The following PROC SQL code counts the unique values of Type and puts each value in a separate macro variable. The SAS macro that follows the PROC SQL code uses these macro variables to create a SAS data set for each value. You do not need to know beforehand how many unique values there are or what the values are.

proc sql noprint;
   select count(distinct type)
      into :n
      from sql.features;
   select distinct type
      into :type1 - :type%left(&n)
      from sql.features;
quit;

%macro makeds;
   %do i=1 %to &n;
      data &&type&i (drop=type);
         set sql.features;
         if type="&&type&i";
      run;
   %end;
%mend makeds;
%makeds;

8
paladin112 发表于 2010-2-9 12:10:54
不错,很巧妙,赞的

9
soporaeternus 发表于 2010-2-9 22:35:01
好厉害的宏啊,学习学习

10
hongxx 发表于 2010-2-23 20:44:41
顺路贴一个把多个外部数据写到同一个sas数据集的程序:http://support.sas.com/kb/24/712.html


--------------------------------------------------------------------------------

/* Create external file EXTFILE1 */  

data _null_;
  file 'c:\temp\extfile1.txt';
  put "05JAN2001 6 W12301 1.59 9.54";                                          
  put "12JAN2001 3 P01219 2.99 8.97";                                            
  put "16JAN2001 1 A00101 3.00 3.00";                                            
  put "19JAN2001 3 A00101 3.00 9.00";                                            
  put "24JAN2001 2 B90035 2.59 5.18";                                            
run;                                                                        
                                                                     
                                                                        
/* Create external file EXTFILE2 */   

data _null_;
  file 'c:\temp\extfile2.txt';
  put "02FEB2001 1 P01219 2.99 2.99";                                       
  put "05FEB2001 3 A00901 1.99 5.97";                                       
  put "07FEB2001 2 C21135 3.00 6.00";                                       
  put "14FEB2001 7 B90035 2.59 18.13";                                       
  put "20FEB2001 6 A00901 1.99 11.94";                                       
  put "27FEB2001 1 W12301 1.59 1.59";                                       
  put "27FEB2001 2 C00300 1.00 2.00";                                       
  put "28FEB2001 2 B90035 2.59 5.18";
run;

/*  Create the external file EXTFILE3 */  
                                                                    
data _null_;
  file 'c:\temp\extfile3.txt';
  put "06MAR2001 4 A00101 3.59 14.36";                                      
  put "12MAR2001 2 P01219 2.99 5.98";                                       
  put "13MAR2001 2 A00101 3.00 6.00";                                       
  put "16MAR2001 3 B90035 2.59 7.77";                                       
  put "16MAR2001 1 W99201 5.50 5.50";                                       
  put "21MAR2001 3 C30660 2.00 6.00";                                       
  put "29MAR2001 5 A00901 1.99 9.95";                                       
run;  

/* Path to files to be read are in the DATALINES.               */
/* Each file is read in turn with the same INPUT statement.     */
/* The END= variable is set to 1 each time the DATA step        */
/* comes to the end of a file.                                  */
/*                                                              */
/* Read the name of the file to be read from the DATALINES and  */
/* store it in FIL2READ.  The file is then read in the DO WHILE */
/* loop.  At the end of the file, the DO loop ends, control     */
/* passes back to the top of DATA step and the process starts   */
/* over again until all files have been read.                   */
/*                                                              */
/* The argument "dummy" in the INFILE statement is a place-     */
/* holder used in place of a file reference.                    */
           
data one;
  infile datalines;

  /* Ensure fully qualified path will fit in FIL2READ */
  length fil2read $40;

  /* Input path of file to be read from DATALINES */
  input fil2read $;

  infile dummy filevar=fil2read end=done;
  do while(not done);

    /* Input statement for files to be read */
    input @1 date date9. @11 quanity item $ price totcost;  
    output;
  end;      
datalines;
c:\temp\extfile1.txt
c:\temp\extfile2.txt
c:\temp\extfile3.txt
;         
            
proc print data=one;
run;

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

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