BookmarkSubscribeRSS Feed
SAS_User
Calcite | Level 5

Hi,

It will be great if somebody could help me out here. I am trying to create this new variable from my existing dataset. In my current dataset I have variables section1 and section2 and other variables var1_1, var_3, var1_4, etc based on the value of the variables section1 and section2. I am trying to create 2 new variables that contain the value of variables var_i, var_j where "i" and "j" are the values of variables section1 and section2.

I don't fully understand how symput works but have the following working code. Somehow hist1 and hist2 only show the value of the corresponding var_k for the very last record in the dataset. And also, how do I convert the macro variable to a variable in the dataset "sections_mod"?

data sections_mod;

set sections end=eof;

if eof then do;

    call symput("num1", trim(compress(Section1))); call symput('Hist1', VAR_&num1); hist1= &Hist1;

    call symput("num2", trim(compress(Section2))); call symput('Hist2', VAR_&num2); hist2= &Hist2;

end ;

run;

%put _user_;

Any thoughts?

Thanks,

Aarthi

3 REPLIES 3
Haikuo
Onyx | Level 15

I don't have answer for your specific question, which requires more information on your data set. Generally speaking, you don't need to create a data set variable from another data set variable using macro variable as a middle man. What 's wrong with just using assignment statement (or plus whatever operators needed) to do the job (new=old)?  In theory, you sure can use call symput to first retrieve values into macro variables and then using symget to put the value back into your data set variables, well, for your scenario, doing that is like traveling from DC to NY via north pole.

just my 2 cents,

Haikuo

Tom
Super User Tom
Super User

Not sure what macro variables have to do with your problem?

If you have a variable, call it NAME, that contains the name of another variable, call it X, and you want to get the value of the variable X there are a few ways.

If your variables names and data are like your example use an array;

array v var1 - var10;

section1=5;

want = v(section1);

Otherwise you might look at the VVALUEX function.

name='X';

x=100;

want = vvaluex(name);

data_null__
Jade | Level 19

I would like to see some example data.  What you have, and example of what you want.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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