楼主: elliott828
18652 9

[原创博文] 如何判断一个变量是否存在 [推广有奖]

  • 0关注
  • 0粉丝

本科生

20%

还不是VIP/贵宾

-

威望
0
论坛币
1 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
890 点
帖子
57
精华
0
在线时间
58 小时
注册时间
2009-5-7
最后登录
2016-6-5

楼主
elliott828 发表于 2010-1-23 06:39:03 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
用在宏里面的。我知道判断一个data set是否存在可以用%sysfunc(exist(data-set name));
但是我现在只想要知道一个data set里面的某个变量是否存在应该用什么程序呢?
比如说data set是sasuser.a,我想要知道a里面的变量b是否存在。这个程序要能放在宏里使用。


我查了一下可以跟%sysfunc的普通程序,好像没有找到,cexist,filexist这样的都不行。拜求!


谢谢各位了!
二维码

扫码加我 拉你入群

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

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

关键词:sysfunc sasuser exist Data ASUS 如何 程序

回帖推荐

yuerqieqie 发表于9楼  查看完整内容

试一试下面的程序 data test1; a = 1; run; data test2; set test1; a = 2; where a = 1; run; 你会发现test2有1行数据,且a=2。说明在读入test1的时候先判断a是否等于1,等于1就读入,然后执行赋值语句a = 2。 同理执行 data test3; set test1; a = 2; where a = 2; run; 你会发现test3没有数据输出,因为在读入test1的时候没有一条数据满足a = 2。

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

See below. %macro checkvar(dsn=, var=); options nonotes; ***Note: 0---not found :: 1---found; proc contents data=&dsn out=_name_(keep=name) noprint; run; %local rc; %let rc=0; data _null_; set _name_; call symputx('rc','1'); where upcase(name)="%upcase(&var)"; run; %put rc=&rc ; %mend; %checkvar(dsn=sashelp.class, var=a ...

popobobo 发表于3楼  查看完整内容

data a; b = 1; c = 2; output; run; %macro existsVariable(lib =, ds =, var =); %local dsid check rc; %let dsid = %sysfunc(open(&lib..&ds.)); %let check = %sysfunc(varnum(&dsid., &var.)); %let rc = %sysfunc(close(&dsid.)); %if &check. = 0 %then %do; 0 %end; %else %do; 1 %end; %mend existsVariable; %put %existsVariable(lib = work, ds = a, var = b); 1# el ...

本帖被以下文库推荐

沙发
Alexander浮云 发表于 2010-1-23 06:55:44
晕,我还以为是计量呢,sorry我准备回答Hypothesis testing to test the significance of parameter。看来没有用了
山高人为峰

藤椅
popobobo 发表于 2010-1-23 10:58:37
data a;
  b = 1; c = 2; output;
run;

%macro existsVariable(lib =, ds =, var =);
  %local dsid check rc;
  %let dsid = %sysfunc(open(&lib..&ds.));
  %let check = %sysfunc(varnum(&dsid., &var.));
  %let rc = %sysfunc(close(&dsid.));
  %if &check. = 0 %then %do;
    0
  %end; %else %do;
    1
  %end;
%mend existsVariable;

%put %existsVariable(lib = work, ds = a, var = b);

1# elliott828
已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
bakoll + 3 + 3 精彩帖子
webgu + 60 + 60 + 1 + 1 + 1 精彩帖子
Eternal0601 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 63  论坛币 + 63  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

板凳
bobguy 发表于 2010-1-23 11:03:39
elliott828 发表于 2010-1-23 06:39
用在宏里面的。我知道判断一个data set是否存在可以用%sysfunc(exist(data-set name));
但是我现在只想要知道一个data set里面的某个变量是否存在应该用什么程序呢?
比如说data set是sasuser.a,我想要知道a里面的变量b是否存在。这个程序要能放在宏里使用。


我查了一下可以跟%sysfunc的普通程序,好像没有找到,cexist,filexist这样的都不行。拜求!


谢谢各位了!
See below.

%macro checkvar(dsn=, var=);
   options nonotes;
   ***Note: 0---not found :: 1---found;
    proc contents data=&dsn out=_name_(keep=name) noprint; run;
     %local rc;
     %let rc=0;

    data _null_;
      set _name_;
      call symputx('rc','1');
      where upcase(name)="%upcase(&var)";
    run;

    %put rc=&rc  ;

%mend;

%checkvar(dsn=sashelp.class, var=age)
%checkvar(dsn=sashelp.class, var=age2)
已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
bakoll + 3 + 3 精彩帖子
webgu + 60 + 60 + 1 + 1 + 1 精彩帖子
Eternal0601 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 63  论坛币 + 63  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

报纸
Eternal0601 发表于 2013-5-9 17:25:00
bobguy 发表于 2010-1-23 11:03
See below.

%macro checkvar(dsn=, var=);
Hi,bobguy,变量age2不在数据集中时,感觉此时的宏变量 rc的值仍然应该是1,但执行的结果是0,不知道是咋回事,望不吝指教,谢谢!
    data _null_;
      set _name_;
      call symputx('rc','1');
      where upcase(name)="%upcase(&var)";
    run;

地板
yuerqieqie 发表于 2013-5-9 21:00:58
Eternal0601 发表于 2013-5-9 17:25
Hi,bobguy,变量age2不在数据集中时,感觉此时的宏变量 rc的值仍然应该是1,但执行的结果是0,不知道是咋回 ...
因为where Statement. 只读入满足where条件的数据。 当where name = “Age2”,没有数据被读入, call symputx('rc','1') 没有执行。rc = 0

7
Eternal0601 发表于 2013-5-9 21:22:26
yuerqieqie 发表于 2013-5-9 21:00
因为where Statement. 只读入满足where条件的数据。 当where name = “Age2”,没有数据被读入, call sy ...
喔,call symputx('rc','1');在where语句前面,只要没有数据读入的话,这句就直接跳过不执行吗,有些诡异,谢谢!

8
yuerqieqie 发表于 2013-5-9 21:30:58
Eternal0601 发表于 2013-5-9 21:22
喔,call symputx('rc','1');在where语句前面,只要没有数据读入的话,这句就直接跳过不执行吗,有些诡异 ...
这是where语句efficiency的地方
For each iteration of the DATA step, the first operation SAS performs in each execution of a SET, MERGE, MODIFY, or UPDATE statement is to determine whether the observation in the input data set meets the condition of the WHERE statement. The WHERE statement takes effect immediately after the input data set options are applied and before any other statement in the DATA step is executed. If a DATA step combines observations using a WHERE statement with a MERGE, MODIFY, or UPDATE statement, SAS selects observations from each input data set before it combines them.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Eternal0601 + 1 + 1 + 1 分析的有道理

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

9
yuerqieqie 发表于 2013-5-9 21:37:32
Eternal0601 发表于 2013-5-9 21:22
喔,call symputx('rc','1');在where语句前面,只要没有数据读入的话,这句就直接跳过不执行吗,有些诡异 ...
试一试下面的程序
data test1;
        a = 1;
run;
data test2;
        set test1;
        a = 2;
        where a = 1;
run;
你会发现test2有1行数据,且a=2。说明在读入test1的时候先判断a是否等于1,等于1就读入,然后执行赋值语句a = 2。
同理执行
data test3;
        set test1;
        a = 2;
        where a = 2;
run;
你会发现test3没有数据输出,因为在读入test1的时候没有一条数据满足a = 2。
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
bakoll + 3 + 3 精彩帖子
Eternal0601 + 1 + 1 + 1 观点有启发

总评分: 经验 + 3  论坛币 + 3  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

10
Eternal0601 发表于 2013-5-9 21:39:55
yuerqieqie 发表于 2013-5-9 21:30
这是where语句efficiency的地方
For each iteration of the DATA step, the first operation SAS perfor ...
明白了,多谢!!!刚用下面的程序试了下,貌似当没有数据读入时,就直接结束data步了,其它所有的语句都不执行了
proc contents data=sashelp.class out=_name_(keep=name) noprint;
run;

data _null_;
  set _name_;
  put "wrong";
  where upcase(name)="AGE2";   
  put "right";
  b=3;
  put "just test";
  put b=;
run;

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

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