Hi Experts,
I have following data in rule column:
s_no | rule |
1 | Security_type in("GSEC", "SSEC") |
2 | Security_type = "OAS" AND Guaranteer = "Cent_govt" |
3 | Security_type = "OAS" AND Guaranteer = "State_govt" AND Residual_maturity LE 0.5 |
4 | Security_type = "OAS" AND Guaranteer = "State_govt" AND Residual_maturity GT 0.5 LE 2 |
5 | Security_type = "OAS" AND Guaranteer = "State_govt" AND Residual_maturity GT 2 |
I want to create 5 macro variable var1 var2 . . var5 which contains
var1: Security_type in("GSEC", "SSEC")
var2: Security_type = "OAS" AND Guaranteer = "Cent_govt"
.
.
and so on.
sample code is here:
%macro mymacro;
proc sql noprint;
select count(rule) into :count from MRCC_SMM.SMM_report_rules where s_no is not null;
quit;
proc sql noprint;
select rule into:var separated by '@' from MRCC_SMM.SMM_report_rules where s_no is not null;
quit;
%do i=1 %to &count;
data _null_;
call symput("var&&i.",%sysfunc(scan("&&var.", &&i., "@")));
run;
%end;
%mend;
%mymacro;
Please give any idea how I can do that?. Thanks In advance.
data _null_;
set have;
call symputx(cats('var',s_no),rule);
run;
Don't use macro code where you need regular SAS code. Remember macro processor is just there to help you generate SAS code. For this problem you don't need any macro code at all. Just a data step.
data _null_;
set MRCC_SMM.SMM_report_rules end=eof;
where not missing(s_no);
call symputx(cats('var',_n_),rule);
if eof then call symputx('count',_n_);
run;
Another option:
proc sql noprint;
select rule into :var1- from mydatasetname;
quit;
%let nvars=&sqlobs;
You don't even have to know how many &var variables will be created, SAS figures it out for you. The number created is in &SQLOBS, which I have stored for later use in &NVARS.
Note: if the numbers in s_no are not consecutive, this may or may not work, depending on what you do with them. The CALL SYMPUTX method that I provided earlier will work if s_no are not consecutive.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: