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!
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;
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;
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.
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.
@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 ...
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.