Help using Base SAS procedures

call symput problem

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

call symput problem

Hi Forum,

I have this dataset.

data CCCC;

input yearmonth age;

cards;

201112 10

201012 20

201207 30

;

run;

I need to pick the latest vlaue of variable named yearmonth (which is 201207) and feed that value into sql code below.

/*SQL code*/

proc sql;

  select age

  from anyname

  where yearmonth = '&latest';

  run;

My approach 

proc sort data=CCCC out= anyname;

      by descending yearmonth;

    run;

    data _null_;

      set anyname;

      if _n_=1 then do;

        call symput('latest',yearmonth);

        stop;

      end;

    run;

problem

An error messege comes.

ERROR: Expression using equals (=) has components that are of different data types.

Could anyone please help me to fix this.

Thanks

Mirisage


Accepted Solutions
Solution
‎08-09-2012 04:31 PM
Valued Guide
Posts: 797

Re: call symput problem


Yearmonth is a numeric variable, but macrovar can only be characters.  You can

   call symput('latest',cats(yearmonth));

which takes advantage of the behavior of the CAT functions to automatically convert number values to character.

Before the advent of the CAT functions, we would

   call symput('lastest',put(yearmonth,8.0-L));

View solution in original post


All Replies
Solution
‎08-09-2012 04:31 PM
Valued Guide
Posts: 797

Re: call symput problem


Yearmonth is a numeric variable, but macrovar can only be characters.  You can

   call symput('latest',cats(yearmonth));

which takes advantage of the behavior of the CAT functions to automatically convert number values to character.

Before the advent of the CAT functions, we would

   call symput('lastest',put(yearmonth,8.0-L));

PROC Star
Posts: 7,363

Re: call symput problem

Mark already answered your question about creating a macro variable, but I have to ask: why not just get the result from a single proc sql run?  e.g.:

proc sql;

  select age

    from CCCC

      having yearmonth = max(yearmonth);

;

quit;

Super User
Posts: 5,082

Re: call symput problem

The WHERE clause will need to use double quotes, not single quotes:

where yearmonth = "&latest";

Macro variable references in single quotes do not get resolved.

Regarding CALL SYMPUT, the earlier comment is correct.  An easier solution would be to add a single letter:

call symputX('latest', yearmonth);

Good luck.

Super Contributor
Posts: 338

Re: call symput problem

Hi Mkeintz, Art and Astounding,

Thank very much every one of you for this excellent clarification.

Hi Art,

What you have suggested is also quite correct.

Warm regards

Mirisage

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 280 views
  • 6 likes
  • 4 in conversation