DATA Step, Macro, Functions and more

SAS drops variable on its own

Accepted Solution Solved
Reply
Super User
Posts: 9,914
Accepted Solution

SAS drops variable on its own

Please run this code:

data have;
input client code $;
cards;
1 A
1 B
2 A
3 A
3 C
3 D
4 A
5 A
5 B
;
run;

data int;
set have (where=(code ne 'A')) ;
val = 1;
run;

proc transpose
  data=int
  out=codes (drop=_name_)
;
by client;
id code;
var val;
run;

data want;
merge
  have (
    in=a
    where=(code = 'A')
  )
  codes
;
by client;
if a;
A = 1;
drop code;
run;

proc print data=want noobs;
run;

I get this result:

client    B    C    D

   1      1    .    .
   2      .    .    .
   3      .    1    1
   4      .    .    .
   5      1    .    .

Why is variable A missing from the dataset want?

Even the log does not have it:

52         
53         data want;
54         merge
55           have (
56             in=a
57             where=(code = 'A')
58           )
59           codes
60         ;
61         by client;
62         if a;
63         A = 1;
64         drop code;
65         run;

NOTE: There were 5 observations read from the data set WORK.HAVE.
      WHERE code='A';
NOTE: There were 3 observations read from the data set WORK.CODES.
NOTE: The data set WORK.WANT has 5 observations and 4 variables.

This happens on SAS 9.4M5 on AIX 7.1

 

Please tell me I'm not going crazy!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

Accepted Solutions
Solution
‎04-03-2018 09:16 AM
Super User
Posts: 6,635

Re: SAS drops variable on its own

Posted in reply to KurtBremser

Well, in= variables get dropped automatically.  So even if you change the value of an in= variable, that won't be sufficient to keep it.

 

It's possible that this is just an oversight that you were reusing the variable.  Maybe this combination will strike you as unusual:

 

if a;

A=1;

View solution in original post


All Replies
Solution
‎04-03-2018 09:16 AM
Super User
Posts: 6,635

Re: SAS drops variable on its own

Posted in reply to KurtBremser

Well, in= variables get dropped automatically.  So even if you change the value of an in= variable, that won't be sufficient to keep it.

 

It's possible that this is just an oversight that you were reusing the variable.  Maybe this combination will strike you as unusual:

 

if a;

A=1;

Super User
Posts: 9,914

Re: SAS drops variable on its own

Posted in reply to Astounding

Of course it was an oversight on my part. Reusing the simple name A was the reason.

This time I really needed another pair of eyeballs.

Thanks very much, I'm still sane.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 7,935

Re: SAS drops variable on its own

Posted in reply to KurtBremser

I always try to use variable names that start with IN for the IN= dataset option. So IN=in1 or IN=in_master instead of things like IN=A.

 

It is still possible to have name conflicts, but at least then the reason might be easier to spot.

Super User
Posts: 9,914

Re: SAS drops variable on its own


@Tom wrote:

I always try to use variable names that start with IN for the IN= dataset option. So IN=in1 or IN=in_master instead of things like IN=A.

 

It is still possible to have name conflicts, but at least then the reason might be easier to spot.


<grin>

I'm used to use simple names like a,b,c for my in= variables because in a production environment there never are such names in datasets. But with simple example code ...

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 81 views
  • 3 likes
  • 3 in conversation