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

proc sql;

alter table _118

   add problem_day1 num add problem_day2 num

   add problem_day3 num add problem_day4 num

   add problem_day5 num add problem_day6 num

   add problem_day7 num add problem_day8 num

   add problem_day9 num add problem_day10 num

   add problem_day11 num add problem_day12 num

   add problem_day13 num add problem_day14 num

   add problem_day15 num add problem_day16 num

   add problem_day17 num add problem_day18 num

   add problem_day19 num add problem_day20 num

   add problem_day21 num add problem_day22 num

   add problem_day23 num add problem_day24 num

   add problem_day25 num add problem_day26 num

   add problem_day27 num add problem_day28 num

   add problem_day29 num add problem_day30 num;

update _118

     set problem_day1=

      case when day=29 then distinct_problem

      else 0

  end;

update _118

  set problem_day2=

      case when day=28 then distinct_problem

     else 0

      end;

update _118

  set problem_day3=

      case when day=27 then distinct_problem

       else 0

  end;

quit;

I have tried to use the macro. But there is an problem that I can not convert the macro variable to numeric  in the sql.

%macro pao;

proc sql;

%DO i=1 %TO 30;

alter table _125

   add video_day&i num;

%END;

%DO k=1 %TO 3;

%let j=%eval(30-&k);

update _125

     set video_day&k=

      case when day=&j then distinct_video

      else 0

  end;

%END;

alter table _125

    drop date ,datelast ,day,distinct_video,date_problem,distinct_problem;

quit;

%mend;

%pao;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

There's nothing wrong with &J and &K, at least not in the macro code that you posted.

There are other mismatches between your code, and your macro.  For example, is the field name DISTINCT_VIDEO or should it be DISTINCT_PROBLEM?

It's too much work to imagine what you might be trying to accomplish.  Why not post an updated version of the desired code you are trying to replace, an updated version of what your macro looks like, and the error message you are getting?

View solution in original post

7 REPLIES 7
TomKari
Onyx | Level 15

Macro variables are always character, but your code seems to use the %eval correctly. What are the symptoms of your problems?

Tom

cecily
Calcite | Level 5

in the when clause, sas could not execute the when clause because  &j is not numeric.

Here, I would like to use the Do loop in the when clause. Because, Each time  ' video_day' is  changed, the when clause also need to be changed.

Could you help me ?

Astounding
PROC Star

It's a little difficult to decipher, since your macro doesn't exactly match your program.  For example, are you trying to alter table _125 or table _118?

At any rate, this section needs to change in two ways:

%DO i=1 %TO 30;

alter table _125

   add video_day&i num;

%END;

It should become:

alter table _125

%DO i=1 %TO 30;

   add video_day&i num

%END;

That means moving the ALTER statement out of the loop, and removing the interior semicolon.

After that, we'll have to see what problems remain.

Good luck.

cecily
Calcite | Level 5

oh.I see. I will change the code and make it better. Actually the main problem is

update _125

     set video_day&k=

      case when day=&j then distinct_video

      else 0

Here, in the when clause, sas could not execute the when clause because  &j is not numeric.

How could I solve this problem?

Astounding
PROC Star

There's nothing wrong with &J and &K, at least not in the macro code that you posted.

There are other mismatches between your code, and your macro.  For example, is the field name DISTINCT_VIDEO or should it be DISTINCT_PROBLEM?

It's too much work to imagine what you might be trying to accomplish.  Why not post an updated version of the desired code you are trying to replace, an updated version of what your macro looks like, and the error message you are getting?

cecily
Calcite | Level 5

There is something wrong.

In the table table _125,  day is numeric. But here   ' when  day=&j    '  I let the numeric variable equals to a character variable . So There is a syntax error  in the code.

Astounding
PROC Star

Descriptions are not enough.  You willl need to post both your macro, and the error message.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1304 views
  • 0 likes
  • 3 in conversation