symget

Accepted Solution Solved
Reply
Valued Guide
Posts: 858
Accepted Solution

symget

I have not used symget in the past and have come across it while studying for the advanced cert.  The book I have and anything I found online are not helping me grasp what this is used for.  If anyone has any documentation or code that shows a good example I would appreciate it.

Thanks!


Accepted Solutions
Solution
‎03-29-2013 04:05 PM
Super Contributor
Posts: 1,636

Re: symget

Hi,

below is dataset stats:

                           Obs    dept        s_sal

                              1                    221000

                              2     bedding     59000

                              3     carpet      41000

                              4     gifts         27000

                              5     kitchen     46000

                              6     tv             48000

after running

data _null_;

   set stats;

   if _n_=1 then call symput('s_tot',s_sal); /* create macro varible s_tot=221000) */

   else call symput('s'||dept,s_sal); /* when _n_ is not 1, then macro 's'||dept is created, here dept is a variable, so it changes */

run;

/*

six macro variables have been created:

macro_variable   value

S_TOT               221000

SKITCHEN        46000

SCARPET          41000

STV                   48000

SGIFTS             27000

SBEDDING        59000

*/

data new;

   set dusty;

   pctdept=(salary/symget('s'||dept))*100; /* symget gets the macro variable values. when dept='bedding', symget('s'||dept) get the value of &SBEDDING ,which is 59000, so for the first observation of dataset dusty, pctdept=18000/59000=30.508 */

   pcttot=(salary/&s_tot)*100;

run;

/*also, add */

%put _user_;

/*to your code help you to see the macro variables you created*/

View solution in original post


All Replies
Super User
Posts: 17,936

Re: symget

SAS Docs:


Details

SYMGET and SYMGETN return values of macro variables when the program executes, whereas "&macro-var" returns values when the program compiles. SYMGET returns the value of a SAS macro variable as a character value and SYMGETN returns the value as a numeric value.

SAS(R) Component Language 9.2: Reference

Respected Advisor
Posts: 3,124

Re: symget

It becomes particular useful when one tries to get values from macro variables dynamically. What it does is to hold resolving of a macro variable until being executed (executing phase), while '&' type of resolving will have the macro variable resolved way before the executing of data step (compiling phase). IMHO, the official help doc has an good example on how/when to use symget():

SAS(R) 9.3 Macro Language: Reference

BTW, Art C is online, he is a published expert on Macro language. Hopefully he would chime in on this.

Haikuo

Valued Guide
Posts: 858

Re: symget

Thanks for the explanation, I appreciate any feedback.  I have seen the example you are referring too but still not getting it.  I'm going to mess around with the code, after I write something it should click.

Thanks!

Valued Guide
Posts: 858

Re: symget

Maybe this will help me grasp it.  Can you look at the code below?  Specifically the:

else call symput('s'||dept,s_sal);

   pctdept=(salary/symget('s'||dept))*100;

Is the symget meant to reverse the solution of the symput?

data dusty;

   input dept $ name $ salary @@;

   datalines;

bedding Watlee 18000    bedding Ives 16000

bedding Parker 9000     bedding George 8000

bedding Joiner 8000     carpet Keller 20000

carpet Ray 12000        carpet Jones 9000

gifts Johnston 8000     gifts Matthew 19000

kitchen White 8000      kitchen Banks 14000

kitchen Marks 9000      kitchen Cannon 15000

tv Jones 9000           tv Smith 8000

tv Rogers 15000         tv Morse 16000

;

proc means noprint;

   class dept;

   var salary;

   output out=stats sum=s_sal;

run;

proc print data=stats;

   var dept s_sal;

   title "Summary of Salary Information";

   title2 "For Dusty Department Store";

run;

data _null_;

   set stats;

   if _n_=1 then call symput('s_tot',s_sal);

   else call symput('s'||dept,s_sal);

run;

data new;

   set dusty;

   pctdept=(salary/symget('s'||dept))*100;

   pcttot=(salary/&s_tot)*100;

run;

proc print data=new split="*";

   label dept   ="Department"

         name   ="Employee"

         pctdept="Percent of *Department* Salary"

         pcttot ="Percent of *   Store  * Salary";

   format pctdept pcttot 4.1;

   title  "Salary Profiles for Employees";

   title2 "of Dusty Department Store";

run;

Solution
‎03-29-2013 04:05 PM
Super Contributor
Posts: 1,636

Re: symget

Hi,

below is dataset stats:

                           Obs    dept        s_sal

                              1                    221000

                              2     bedding     59000

                              3     carpet      41000

                              4     gifts         27000

                              5     kitchen     46000

                              6     tv             48000

after running

data _null_;

   set stats;

   if _n_=1 then call symput('s_tot',s_sal); /* create macro varible s_tot=221000) */

   else call symput('s'||dept,s_sal); /* when _n_ is not 1, then macro 's'||dept is created, here dept is a variable, so it changes */

run;

/*

six macro variables have been created:

macro_variable   value

S_TOT               221000

SKITCHEN        46000

SCARPET          41000

STV                   48000

SGIFTS             27000

SBEDDING        59000

*/

data new;

   set dusty;

   pctdept=(salary/symget('s'||dept))*100; /* symget gets the macro variable values. when dept='bedding', symget('s'||dept) get the value of &SBEDDING ,which is 59000, so for the first observation of dataset dusty, pctdept=18000/59000=30.508 */

   pcttot=(salary/&s_tot)*100;

run;

/*also, add */

%put _user_;

/*to your code help you to see the macro variables you created*/

Valued Guide
Posts: 858

Re: symget

Thank you very much, as always you are a huge help.

Have a great weekend.  With  your help maybe I'll get through the advanced too Smiley Happy

Super Contributor
Posts: 1,636

Re: symget

Congratulations On passing your base exam and good luck with the advanced exam!!!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 412 views
  • 0 likes
  • 4 in conversation