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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star


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));

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star


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));

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
art297
Opal | Level 21

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;

Astounding
PROC Star

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.

Mirisage
Obsidian | Level 7

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1420 views
  • 6 likes
  • 4 in conversation