Help using Base SAS procedures

Using SYMPUTX/SYMGET

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Using SYMPUTX/SYMGET

Hello all,

I'm trying to automate my program in which it will select the observation with the highest salary using SYMPUTX and SYMGET and simply replace the macro variable to the correct value.  However, the macro value isn't be replaced, rather going to eof.  Although I do get the 1 correct observation (4     D     $87,545) returned I'm trying to keep all original 5 obs in the data set.  I think I'm butchering the syntax.  Any idea?

Thanks,

David

ID          Name               Salary

1          A                        $20,000

2          B                        $30,475

3          C                        $26,895

4          D                        $87,975

5          E                        $42,545

data salary;

     set Emp_Payroll end=final;

     call symputx('highsal',employee_id,salary);

     call symputx('empid',employee_id);

     if salary>symget('highsal');

     if final then do;

          call symputx('highsal',salary);

     end;

run;


Accepted Solutions
Solution
‎03-31-2015 12:41 AM
Occasional Contributor
Posts: 16

Re: Using SYMPUTX/SYMGET

Thanks for information.  My goal was to try and avoid using the proc sort and have the macro variable overwrite.  So, I guess I'm really trying to compare the previous salary and, if higher, overwrite that value to the salary macro.

I do appreciate your insight.

David

View solution in original post


All Replies
Contributor dkb
Contributor
Posts: 53

Re: Using SYMPUTX/SYMGET

Could you post your log, please?  When I run your code, I get 5 observations on the table and the wrong high value.

Code:

data Emp_Payroll;

input employee_ID Name $ Salary;

datalines;

1          A                        20000

2          B                        30475

3          C                        26895

4          D                        87975

5          E                        42545

;

run;

data salary;

     set Emp_Payroll end=final;

     call symputx('highsal',employee_id,salary);

     call symputx('empid',employee_id);

     if salary>symget('highsal');

     if final then do;

          call symputx('highsal',salary);

     end;

run;

%put &highsal. &empid.;

Log:

25         data salary;

26              set Emp_Payroll end=final;

27              call symputx('highsal',employee_id,salary);

28              call symputx('empid',employee_id);

29              if salary>symget('highsal');

30              if final then do;

31                   call symputx('highsal',salary);

32              end;

33         run;

NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).

      27:41  

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).

      29:16  

NOTE: There were 5 observations read from the data set WORK.EMP_PAYROLL.

NOTE: The data set WORK.SALARY has 5 observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.09 seconds

      cpu time            0.03 seconds

     

34         %put &highsal. &empid.;

42545 5

Trusted Advisor
Posts: 1,131

Re: Using SYMPUTX/SYMGET

could you please try the below code. To get the highest salary employee details along with salary we need to sort the data by salary.

data Emp_Payroll;

input ID$           Name$               Salary :dollar8.;

cards;

1          A                        $20,000

2          B                        $30,475

3          C                        $26,895

4          D                        $87,975

5          E                        $42,545

;

proc sort data=Emp_Payroll;

by Salary;

run;

data salary;

     set Emp_Payroll end=final;

     if final then do;

         call symputx("empid",id);

         new=symget("empid");

         call symputx("highsal",salary);

     end;

run;

%put &empid &highsal ;

Thanks,

Jag

Thanks,
Jag
Solution
‎03-31-2015 12:41 AM
Occasional Contributor
Posts: 16

Re: Using SYMPUTX/SYMGET

Thanks for information.  My goal was to try and avoid using the proc sort and have the macro variable overwrite.  So, I guess I'm really trying to compare the previous salary and, if higher, overwrite that value to the salary macro.

I do appreciate your insight.

David

Super User
Posts: 5,099

Re: Using SYMPUTX/SYMGET

Well, some of the syntax is clearly not what you actually ran.  The first CALL SYMPUTX uses SALARY as the third argument ... certainly not what you intended.

Why all this macro language?  Why not a simpler version:

data salary;

     set Emp_Payroll end=final;

     retain highsal;

     if salary > highsal then highsal = salary;

     if final then do;

          call symputx('highsal', highsal);

     end;

run;

Also note that it is not clear whether SALARY is numeric or character in your data.  If it is character, $9,000 would be greater than all the salaries you have shown and this would probably be the wrong answer.  Even if you don't have salaries that low, as a character string $100,000 would be smaller than all the salaries you have shown which would again give you the wrong answer.

Good luck.

Occasional Contributor
Posts: 16

Re: Using SYMPUTX/SYMGET

Correct, not what I actually ran.  I abbreviated to prevent employee information from being compromised thus not an accurate log to provide.  This is certainly more concise but I should have been more clear as to what my intended goal is...to overwrite the higher salary value when comparing the previous iteration.  I'm all ears if you would know how best to compare the first ob to the next and if that salary is higher have it overwrite the macro in SYMPUTX.

Thanks for the help.

David

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 293 views
  • 6 likes
  • 4 in conversation