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

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.

 

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
  • 816 views
  • 3 likes
  • 3 in conversation