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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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