如何循环遍历一个 %let 语句的列表?

huangapple go评论81阅读模式
英文:

How can I loop through a list of %let statements?

问题

我有五组%let语句需要循环遍历。我一直在注释掉每一组,但我刚意识到我还需要对其他大约30组进行相同的操作。以下是我需要循环遍历的五组:

%let test_code = 5622;
%let total_categories = 5;
%let cat_1_name = 学生作为学习者;
%let cat_2_name = 教学过程;
%let cat_3_name = 评估;
%let cat_4_name = 专业发展、领导力和社区;
%let cat_5_name = 教学场景分析;

%let test_code = 5004;
%let total_categories = 3;
%let cat_1_name = 美国历史、政府、公民权利;
%let cat_2_name = 地理学、人类学、社会学;
%let cat_3_name = 世界历史、经济学;

%let test_code = 5003;
%let total_categories = 3;
%let cat_1_name = 数字与运算;
%let cat_2_name = 代数思维;
%let cat_3_name = 几何与测量、数据、统计与概率;

%let test_code = 5002;
%let total_categories = 2;
%let cat_1_name = 阅读;
%let cat_2_name = 写作、口语、听力;

%let test_code = 5005;
%let total_categories = 3;
%let cat_1_name = 地球科学;
%let cat_2_name = 生命科学;
%let cat_3_name = 物理科学;

其余的代码使用total_categories变量根据每个测试中包含的类别总数进行格式化。如果这已经被问过了,我很抱歉。任何建议都将不胜感激。

英文:

I have five groups of %let statements I would like to loop through. I have been commenting out each group but I just realized I have to do this for about 30 other groups. Here are the five groups I need to loop through:

%let test_code = 5622;
%let total_categories = 5;
%let cat_1_name = Students as Learners;
%let cat_2_name = Instructional Process;
%let cat_3_name = Assessment;
%let cat_4_name = Professional Development, Leadership, and Community;
%let cat_5_name = Analysis of Instructional Scenarios;

%let test_code = 5004;
%let total_categories = 3;
%let cat_1_name = U.S. History, Government, Citizenship;
%let cat_2_name = Geography, Anthropology, Sociology;
%let cat_3_name = World History, Economics;

%let test_code = 5003;
%let total_categories = 3;
%let cat_1_name = Numbers and Operations;
%let cat_2_name = Algebraic Thinking;
%let cat_3_name = Geometry and Measurement, Data, Statistics, and Probability;

%let test_code = 5002;
%let total_categories = 2;
%let cat_1_name = Reading;
%let cat_2_name = Writing, Speaking, Listening;

%let test_code = 5005;
%let total_categories = 3;
%let cat_1_name = Earth Science;
%let cat_2_name = Life Science;
%let cat_3_name = Physical Science;

The rest of the code uses the total_categories variable to format based on the total number of categories included in each test. Apologies if this has already been asked. Any advice is appreciated.

答案1

得分: 1

你的问题表明你已经准备好从简单使用宏变量转向使用宏了。如果你对宏不熟悉,可以将宏视为一段要多次执行的代码块,每次运行时你都可以为代码中的宏变量提供不同的值。下面是一个示例,展示了一个宏,它会运行 PROC PRINT,并允许你在运行宏时为七个宏变量指定值。这些宏变量在 TITLE 语句中使用。

%macro dosomething
  (test_code=
  ,total_categories=
  ,cat_1_name=
  ,cat_2_name=
  ,cat_3_name=
  ,cat_4_name=
  ,cat_5_name=
   )
;

title1 "test_code=&test_code total_categories=&total_categories" ;
title2 "cat_1_name=&cat_1_name cat_2_name=&cat_2_name cat_3_name=&cat_3_name cat_4_name=&cat_4_name cat_5_name=&cat_5_name" ;

proc print data=sashelp.class ;
run ;

title1 ;

%mend dosomething ;

运行这段代码时,你不会得到任何结果。SAS会编译这个宏。你可以像下面这样运行宏:

options mprint ;

%dosomething(
  test_code = 5622
 ,total_categories = 5
 ,cat_1_name = Students as Learners
 ,cat_2_name = Instructional Process
 ,cat_3_name = Assessment
 ,cat_4_name = %str(Professional Development, Leadership, and Community)  /*%str is needed to hide the commas in the value*/
 ,cat_5_name = Analysis of Instructional Scenarios
) 

你也可以再次运行它,像这样:

%dosomething(
  test_code = 5004
 ,total_categories = 3
 ,cat_1_name = %str(U.S. History, Government, Citizenship)
 ,cat_2_name = %str(Geography, Anthropology, Sociology)
 ,cat_3_name = %str(World History, Economics)
) 

如果你需要运行这段代码5次,你可以像上面那样编写5个宏调用。如果你需要运行这段代码30次,你可以像上面那样编写30个宏调用。或者你可以研究一下将宏变量的值存储在数据集中,然后使用该数据集来驱动30个宏调用的方法。这是一种更高级的技术,可以使用 CALL EXECUTE 或其他方法来实现。

英文:

Your question suggests that you're ready to move on from simply using macro variables, to using macros. If you're not familiar with macros, you can think of a macro as a block of code that you want to execute multiple times, and each time you run it you want to provide different values for macro variables in the code. So here's an example of a macro that will run PROC PRINT, that allows you to specify values for seven macro variables when you run the macro. The macro variables are used in the TITLE statement.

%macro dosomething
  (test_code=
  ,total_categories=
  ,cat_1_name=
  ,cat_2_name=
  ,cat_3_name=
  ,cat_4_name=
  ,cat_5_name=
   )
;
   
title1 "test_code=&test_code total_categories=&total_categories" ;
title2 "cat_1_name=&cat_1_name cat_2_name=&cat_2_name cat_3_name=&cat_3_name cat_4_name=&cat_4_name cat_5_name=&cat_5_name" ;

proc print data=sashelp.class ;
run ;

title1 ;

%mend dosomething ;

When you run that code you won't get any results. SAS compiles the macro. You can then run the macro like:

options mprint ;

%dosomething(
  test_code = 5622
 ,total_categories = 5
 ,cat_1_name = Students as Learners
 ,cat_2_name = Instructional Process
 ,cat_3_name = Assessment
 ,cat_4_name = %str(Professional Development, Leadership, and Community)  /*%str is needed to hide the commas in the value*/
 ,cat_5_name = Analysis of Instructional Scenarios
) 

And you can run it for the second time like:

%dosomething(
  test_code = 5004
 ,total_categories = 3
 ,cat_1_name = %str(U.S. History, Government, Citizenship)
 ,cat_2_name = %str(Geography, Anthropology, Sociology)
 ,cat_3_name = %str(World History, Economics)
) 

If you need to run your block of code 5 times, you can write 5 macro calls like above. If you need to run the block of code 30 times, you could write 30 macro calls like above. Or you can look into ways to store the values for the macro variables in a dataset, and then use that dataset to drive 30 macro calls. This is a more advanced technique, it can be done with CALL EXECUTE or other methods.

huangapple
  • 本文由 发表于 2023年8月9日 05:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定