SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Siddhu
Fluorite | Level 6

Please go through the pdf file for full details. 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

@Siddhu welcome to the SAS Community 🙂 Next time you post a question, please do not specify your request in an image file. Describe your problem and provide usable code. Makes it much easier to help you.

 

Here you go though 

 

data have;
input id type $ A B C;
datalines;
1 A 10 100 200
2 B 15 105 300
3 C 20 120 200
4 B 30 125 300
5 A 25 110 400
;

data want;
    set have;
    array _{*} A--C;
    do i=1 to dim(_);
        if vname(_[i])=type then value=_[i];
    end;
    keep id type value;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

@Siddhu welcome to the SAS Community 🙂 Next time you post a question, please do not specify your request in an image file. Describe your problem and provide usable code. Makes it much easier to help you.

 

Here you go though 

 

data have;
input id type $ A B C;
datalines;
1 A 10 100 200
2 B 15 105 300
3 C 20 120 200
4 B 30 125 300
5 A 25 110 400
;

data want;
    set have;
    array _{*} A--C;
    do i=1 to dim(_);
        if vname(_[i])=type then value=_[i];
    end;
    keep id type value;
run;
Siddhu
Fluorite | Level 6

Can you please explain me about the background work of "vname" in SAS arrays. If possible, please try to explain it by a video or post a link as I couldn't get it.

PeterClemmensen
Tourmaline | Level 20

Whenever you encounter a function in SAS you're unsure of, do consult the documentation.

 

in this case

 

SAS Documentation: VNAME Function

Ksharp
Super User

Why not use VVALUEX()  ?

 

data want2;
 set have;
 value=vvaluex(type);
run;
PeterClemmensen
Tourmaline | Level 20

@Ksharp tbh, didn't even cross my mind 🙂 Kudos!

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

data have;
input id type$ a b c d;
cards;
1 A 1 2 3 4
2 B 1 2 3 4
3 C 1 2 3 4
;

data want;
set have;
array vars(*) a b c d ;
do i = 1 to dim(vars);
if lowcase(type)=lowcase(vname(vars(i))) then value=vars(i);
end;
run;
Thanks,
Jag

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1740 views
  • 2 likes
  • 4 in conversation