Hello,
I have a dataset in which each line is a subject, and I'm interested in creating a new variable that contains a list of 40 possible variables, but only the ones that are missing for each subject. Is there an existing macro for this task? Can anyone help me figure out how to create a variable that contains a list of variables that is unique to each line in a dataset?
try:
improved version:
data have;
input id $ v1 v2 v3 v4;
cards;
a 1 2 3 .
b 2 3 . .
c 2 . . .
;
data want;
length new $ 50;
set have;
array _v(*) _numeric_;
do _n_=1 to dim(_v);
if _v(_n_)=. then new=catx('-',new,vname(_v(_n_)));
end;
proc print;run;
Linlin
Message was edited by: Linlin
are the variables numeric or character or both?
They are all numeric.
try:
improved version:
data have;
input id $ v1 v2 v3 v4;
cards;
a 1 2 3 .
b 2 3 . .
c 2 . . .
;
data want;
length new $ 50;
set have;
array _v(*) _numeric_;
do _n_=1 to dim(_v);
if _v(_n_)=. then new=catx('-',new,vname(_v(_n_)));
end;
proc print;run;
Linlin
Message was edited by: Linlin
I needed to get rid of the retain. Otherwise I was getting repeated variable names in the list, but otherwise, this is perfect. Exactly what I needed. Thank you!
Hi,
"call missing (your-variable)" gets rid of the missing variable names from previous IDs.
Hi Linlin, since you create a new list for each observation, you don't need the retain, the call missing() or the output statements. It works just as well without them.
PG
Thank you PG!!!
improved version:
data have;
input id $ v1 v2 v3 v4;
cards;
a 1 2 3 .
b 2 3 . .
c 2 . . .
;
data want;
length new $ 50;
set have;
array _v(*) _numeric_;
do _n_=1 to dim(_v);
if _v(_n_)=. then new=catx('-',new,vname(_v(_n_)));
end;
proc print;run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.