BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aklr
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

View solution in original post

7 REPLIES 7
Linlin
Lapis Lazuli | Level 10

are the variables numeric or character or both?

aklr
Calcite | Level 5

They are all numeric.

Linlin
Lapis Lazuli | Level 10

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

aklr
Calcite | Level 5

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!

Linlin
Lapis Lazuli | Level 10

Hi,

"call missing (your-variable)" gets rid of the missing variable names from previous IDs.

PGStats
Opal | Level 21

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

PG
Linlin
Lapis Lazuli | Level 10

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;

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 833 views
  • 3 likes
  • 3 in conversation