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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.