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

Hello, I have a simple problem I would apprecieate any help with.

 

Suppose in my dataset I have a variable called TYPE which is A, B, C, or D. Furthermore I have four additional variable xA, xB, xC, xD. What I would like to do is create a new variable x that has the appropriate xA, xB, etc value depending on the TYPE. So if TYPE=A then x=xA, etc. Here is an example table;

 

TYPE xA xB xC xD x
A 1 3 0 2 1
D 0 9 5 3 3
C 4 2 0 0 0

 

 

I've tried x=x||TYPE and call execute('x=x'||TYPE||';'); but to no avail. I read that you can use some combination of symput and symget.

 

At worst I can do if TYPE='A' then x=XA, etc. but this is really inefficent code (I have 20 different TYPE letters).

 

Is there an elegant solution for this?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

VVALUEX function is what you're after. It returns it as a character though, so you need to convert it to numeric, if required.

 

 

x=vvaluex("x"|Type);
data want;
input type $ xA xB xC xD;
x=vvaluex("x"||type);
cards;
A 1 3 0 2
D 0 9 5 3
C 4 2 0 0 
;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

VVALUEX function is what you're after. It returns it as a character though, so you need to convert it to numeric, if required.

 

 

x=vvaluex("x"|Type);
data want;
input type $ xA xB xC xD;
x=vvaluex("x"||type);
cards;
A 1 3 0 2
D 0 9 5 3
C 4 2 0 0 
;
run;
jklaverstijn
Rhodochrosite | Level 12

This seems like an excellent oppportunity to use arrays. Image to arrays, one temporary with 'A', 'B', etc. The other one with XA, XB, ...

The loop up the index of TYPE in the first and use that index to assign  the value of X to the variable in the second array with the found index.

 

data want;
    array types[4] $ _temporary_  ('A' 'B' 'C' 'D');
    array xs[*] xa xb xc xd;
    type='D';
    x=100;
    do i=1 to dim(types);
      if type=types[i] then xs[i]=x;
    end;
    put _all_;
run;

Hope this gets you on your way,

- Jan

jklaverstijn
Rhodochrosite | Level 12

Oh rats. I did it the other way round. Looks like @Reeza has the proper solution. Smiley Happy

 

- Jan.

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
  • 3 replies
  • 942 views
  • 0 likes
  • 3 in conversation