nested sysfunc + cat + leading 0 removed - sas

I'm trying with the below loop to manage a dynamic append between three tables.
However, the nested cat with %sysfuncs removed the zeros from the month and the day
Would be someone so gentle to explain what Is not working fine here?
I'm expecting to dynamically declares the table like, ex.
"FTP.TOTAL_4B_20170603" (yyyymmdd) and not like the code is retriving: "FTP.TOTAL_4B_201763"
data dd; %MACRO H;
%DO I=1 %TO 2;
proc append
base=prod0
data=FTP.TOTAL_4B_%sysfunc(CATS(%sysfunc(year(%sysfunc(intnx(day,%sysfunc(today()),-&i,s)))),
%sysfunc(putn(%sysfunc(month(%sysfunc(intnx(day,%sysfunc(today()),-&i,s)))),z2.)),
%sysfunc(putn(%sysfunc(day(%sysfunc(intnx(day,%sysfunc(today()),-&i,s)))),z2.))));
run;
%end;
proc sort data=prod0;
by pan fecha;
run;
%MEND H;
%H;
run;
Thanks for your help
Bests
D

Not sure why you have proc append and proc sort within a data step, but I think the macro expression below could help:
%MACRO J;
%DO I=1 %TO 2;
%put FTP.TOTAL_4B_%sysfunc(intnx(DAY,%sysfunc(today()),-&I.,S),yymmddn8.);
%END;
%MEND J;
%J;

You can use formats, to get the date YYYYMMDD. Also intnx is not necessary if you are going to iterate on days only. date() returns integer, so you can easily use addition/subtraction.
%MACRO test;
%DO I=1 %TO 2;
%let test_date=%sysfunc(putn(%sysfunc(date()) - &i, yymmddn8.));
%put DS name FTP.TOTAL_4B_&test_date;
%end;
%MEND test;
%test;
Gives the result
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
59
60
61
62
63 %MACRO test;
64 %DO I=1 %TO 2;
65 %let test_date=%sysfunc(putn(%sysfunc(date())-&i, yymmddn8.));
66 %put DS name FTP.TOTAL_4B_&test_date;
67 %end;
68 %MEND test;
69
70 %test;
DS name FTP.TOTAL_4B_20170612
DS name FTP.TOTAL_4B_20170611
71
72 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
84

Related

how can I build a loop for macro in SAS?

I want to do a simulation based on macro in SAS. I can build a function named 'fine()', the code is as follows
DATA CLASS;
INPUT NAME $ SEX $ AGE HEIGHT WEIGHT;
CARDS;
ALFRED M 14 69.0 112.5
ALICE F 13 56.5 84.0
BARBARA F 13 65.3 98.0
CAROL F 14 62.8 102.5
HENRY M 14 63.5 102.5
RUN;
PROC PRINT;
TITLE 'DATA';
RUN;
proc print data=CLASS;run;
PROC FCMP OUTLIB = work.functions.func;
function populationCalc(HEIGHT,WEIGHT,thres);
pop=HEIGHT-WEIGHT-thres;
return (pop);
ENDSUB;
options cmplib=(work.functions);
%macro fine(i);
data ex;
set CLASS;
thres=&i;
pop = populationCalc(HEIGHT,WEIGHT,thres);
if (pop>50) then score=1;
else score=0;
run;
proc iml;
USE ex;
READ all var _ALL_ into ma[colname=varNames];
CLOSE ex;
nn=nrow(ma);
total_score=sum(ma[,'thres']);
avg_score=sum(ma[,'thres'])/nn;
print total_score avg_score;
%mend fine;
%fine(10);
%fine(100);
%fine(150);
I want to build a loop for function 'fine()' ans also use macro, but the result is not as I expect. How can I fix this?
%macro ct(n);
data data_want;
%do i=1 %to &n;
x=%fine(&i);
output x;
%end;
run;
%macro ct;
%ct(10);
%fine does not generate any text that can be used in the context of a right hand side (RHS) of a DATA Step variable assignment statement.
You seem to perhaps want this data set as a result of invoking %ct
i total_score average_score
- ----------- -------------
1 5 1
2 10 2
3 15 3
etc...
Step 1. Save IML result
Add this to the bottom of IML code in %fine, replacing the print
create fine_out var {total_score avg_score};
append;
close fine_out;
quit;
Step 2. Rewrite ct macro
Invoke %fine outside a DATA step context so the DATA and IML steps can run. Append the IML output to a results data set.
%macro ct(n,out=result);
%local i;
%do i=1 %to &n;
%fine(&i)
%if &i = 1 %then %do;
data &out; set fine_out; run;
%end;
%else %do;
proc append base=&out data=fine_out; run;
%end;
%end;
%mend;
options mprint;
%ct(10)
This should be the output WORK.RESULT based on your data

Include macro condition in report

Below is the small piece of code to get reports in excel.
%if &linear %then %do;
ods excel options(sheet_name="vol");
proc print data=perf;
id direction segment;
var accts;
run;
%end;
%else %do;
ods excel options(sheet_name="vol");
proc print data=perf;
id direction segment;
var accts;
run;
%end;
Direction segment accts
A model 17177
A booked 567
A unbooked 5676
B model 17177
B booked 567
B unbooked 5676
If segments are not available i will get report as below
Direction segment accts
A model 17177
A 1 17177
B model 17177
B 1 17177
Iam planing to introduce two macro variables
%let dir =A;
%let Non_segment=y;
Based on value for direction it should give only those direction and if there no segment(Non_segment=y;), it should have only first observation. So the output will looks like below for Non_segment=y
Direction segment accts
A model 17177
If you do not have any variable to test then you cannot subset to the first observation per by group with just a WHERE statement. You will need to generate a data step.
data to_print ;
set perf ;
by direction;
%if %length(&dir) %then %do;
where direction="&dir";
%end;
%if %upcase(&non_segment)=Y %then %do;
if first.direction;
%end;
run;
I tried below code.
%macro isblank(var);
%if %symexist(&var) %then 1; %*not exist*;
%else %if %sysevalf(%superq(&var)=,boolean) %then 1; %*blank*;
%else 0;
%mend isblank;
%let dir =A;
%let Non_segment=y;
proc print data=new;
%if %isblank(Non_segment) %then (obs=1);*firstobs*;
id direction segment;
var acct;
%else;*all obsevation*;
proc print data =new;
id direction segment;
var acct;
run;
Getting below error
proc print data=new;
78 %if %isblank(Non_segment) %then (obs=1);*firstobs*;
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
79 id direction segment;
80 var acct;
81 %else;*all obsevation*;
ERROR: The %ELSE statement is not valid in open code.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 6 observations read from the data set WORK.NEW.
NOTE: PROCEDURE PRINT used (Total process time):
real time 10.57 seconds
cpu time 6.31 seconds
82 proc print data =new;
83 id direction segment;
84 var acct;
85 run;

space separated list intersection

Note: code edited after remarks from #user667489 but issues remain.
I built a fairly simple macro to return the intersection of 2 space separated lists as a new space separated list but for some reason the definition of the macro returns errors.
The macro loops through both lists and keep an element if a match is found (very straightforward, no handling of duplicates or optimization).
I cannot make sense of the log, which shows a combination of following error messages:
ERROR: Macro keyword LET appears as text.
ERROR: Macro keyword MACRO appears as text.
%macro list_intersection
(list1= /* space separated list, or unique term */
,list2= /* space separated list, or unique term */
);
%local output;
%local i;
%local j;
%let i = 1;
%let j = 1;
%do %while (%length(%scan(&list1,&i)));
%do %while (%length(%scan(&list2,&j)));
%if (%scan(&list1,&i) = %scan(&list2,&j)) %then
%let output = &output %scan(&list1,&i);
%let j = %eval(&j+1);
%end;
%let i = %eval(&i+1);
%end;
&output
%mend;
Can you help me circling the issue ?
I'm also open to a more efficient/robust/simple way of achieving the same output.
Reproducible log
Using SAS 9.3, I put above code in a separate program for it not to be polluted, save project, close and reopen. Open program, click run button, and here is the complete log:
1 The SAS System 09:50 Monday, January 22, 2018
1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Program3';
ERROR: Macro keyword LET appears as text.
4 %LET _CLIENTPROJECTPATH='F:\CI\Projects\Wealth Indicators\106 Explore DIM_PHYSICALPERSON\SAS\Rework_table_auto2.egp';
ERROR: Macro keyword LET appears as text.
5 %LET _CLIENTPROJECTNAME='Rework_table_auto2.egp';
ERROR: Macro keyword LET appears as text.
6 %LET _SASPROGRAMFILE=;
ERROR: Macro keyword LET appears as text.
7
8 ODS _ALL_ CLOSE;
9 OPTIONS DEV=ACTIVEX;
10 GOPTIONS XPIXELS=0 YPIXELS=0;
11 FILENAME EGSR TEMP;
12 ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR STYLE=HtmlBlue
12 ! STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/x86/SASEnterpriseGuide/5.1/Styles/HtmlBlue.css") NOGTITLE NOGFOOTNOTE
12 ! GPATH=&sasworklocation ENCODING=UTF8 options(rolap="on");
13
14 GOPTIONS ACCESSIBLE;
15 %macro list_intersection
ERROR: Macro keyword MACRO appears as text.
16 (list1= /* space separated list, or unique term */
17 ,list2= /* space separated list, or unique term */
18 );
19 %local output;
ERROR: Macro keyword LOCAL appears as text.
20 %local i;
ERROR: Macro keyword LOCAL appears as text.
21 %local j;
ERROR: Macro keyword LOCAL appears as text.
22 %let i = 1;
ERROR: Macro keyword LET appears as text.
23 %let j = 1;
ERROR: Macro keyword LET appears as text.
24 %do %while (%length(%scan(&list1,&i)));
ERROR: Macro keyword DO appears as text.
25 %do %while (%length(%scan(&list2,&j)));
ERROR: Macro keyword DO appears as text.
26 %if (%scan(&list1,&i) = %scan(&list2,&j)) %then
ERROR: Macro keyword IF appears as text.
27 %let output = &output %scan(&list1,&i);
28 %let j = %eval(&j+1);
ERROR: Macro keyword LET appears as text.
29 %end;
ERROR: Macro keyword END appears as text.
30 %let i = %eval(&i+1);
ERROR: Macro keyword LET appears as text.
31 %end;
ERROR: Macro keyword END appears as text.
32 &output
33 %mend;
ERROR: Macro keyword MEND appears as text.
34
35 GOPTIONS NOACCESSIBLE;
36 %LET _CLIENTTASKLABEL=;
ERROR: Macro keyword LET appears as text.
37 %LET _CLIENTPROJECTPATH=;
2 The SAS System 09:50 Monday, January 22, 2018
ERROR: Macro keyword LET appears as text.
38 %LET _CLIENTPROJECTNAME=;
ERROR: Macro keyword LET appears as text.
39 %LET _SASPROGRAMFILE=;
ERROR: Macro keyword LET appears as text.
40
41 ;*';*";*/;quit;run;
42 ODS _ALL_ CLOSE;
43
44
45 QUIT; RUN;
46
Initial code before edit:
%macro list_intersection
(list1= /* space separated list, or unique term */
,list2= /* space separated list, or unique term */
);
%local output =;
%local i = 1;
%local j = 1;
%do %while (%length(%scan(&list1,&i)));
%do %while (%length(%scan(&list2,&j)));
%if (%scan(&list1,&i) = %scan(&list2,&j) %then
%local output = &output %scan(&list1,&i);
%let j = %eval(&j+1);
%end;
%let i = %eval(&i+1);
%end;
&output
%mend;
A few things immediately stand out:
You cannot use %local to set a value for a macro variable. Instead of %local i=1; you must write two separate statements: %local i; %let i = 1;. %local initialises macro variables to an empty string.
You have unbalanced brackets in your %if statement.
Try moving %let j = %eval(&j+1); into the outer %do %while loop.
Also, you probably want to make sure that %scan only uses space as a delimiter - it defaults to space plus . < ( + & ! $ * ) ; ^ - / , % |
Here's a working version:
%macro list_intersection
(list1= /* space separated list, or unique term */
,list2= /* space separated list, or unique term */
);
%local output;
%local i;
%local j;
%let i = 1;
%do %while (%length(%scan(&list1,&i,%str( ))));
%let j = 1;
%do %while (%length(%scan(&list2,&j,%str( ))));
%if %scan(&list1,&i,%str( )) = %scan(&list2,&j,%str( )) %then
%let output = &output %scan(&list1,&i,%str( ));
%let j = %eval(&j+1);
%end;
%let i = %eval(&i+1);
%end;
&output
%mend;
%put %list_intersection(list1=1 2 3,list2=2 3 4);
You can use SAS functions to make that much easier. Use the COUNTW() function to find the upper bound for the %DO loop. Use the FINDW() function to test if word is found in the other list.
%macro list_intersection
(list1 /* space separated list of terms */
,list2 /* space separated list of terms */
);
%local output i next ;
%do i=1 %to %sysfunc(countw(&list1,%str( ))) ;
%let next=%scan(&list1,&i,%str( ));
%if %sysfunc(findw(&list2,&next,,s)) %then %let output=&output &next ;
%end;
&output
%mend;
You could include the i modifier in the findw() call to make it case insensitive. You could also test if the word is already in the output string to eliminate duplicates.
%macro list_intersection_nodups
(list1 /* space separated list of terms */
,list2 /* space separated list of terms */
);
%local output i next ;
%do i=1 %to %sysfunc(countw(&list1,%str( ))) ;
%let next=%scan(&list1,&i,%str( ));
%if %sysfunc(findw(&list2,&next,,si)) and not %sysfunc(findw(&output,&next,,si))
%then %let output=&output &next ;
%end;
&output
%mend;
Example:
274 %put %list_intersection_nodups(A B a C,a c d);
A C

Proc Sql Do Loop in SAS

For an assignment I am asked to create a do loop in Proc Sql statement. My program is not recognizing the m1sales and m2sales. Here are the data sets and the macros that I had to create. The First macro is to set allow people to set the qtr to a number. The second macro set Months One, Two, and Three depending on the months. The first proc sql is doing what I want. The second is not when I add in the do statement. I was given the additional statement, "The %do loop isn't 'within' a create table command. The %do loop is replaced with a sequence of text that follows the create table command; that text may be though of as 'within' the create table statement." Can anyone tell me how to create that do loop correctly?
data Month1;
input Name $ sales;
cards;
Joyce 235
Marsha 352
Bill 491
Vernon 210
Sally 418
;
data Month2;
input Name $ sales;
cards;
Joyce 169
Marsha 281
Bill 315
Vernon 397
Sally 305
;
data Month3;
input Name $ sales;
cards;
Joyce 471
Marsha 314
Bill 394
Vernon 291
Sally 337
;
data Month4;
input Name $ sales;
cards;
Joyce 338
Marsha 259
Bill 310
Vernon 432
Sally 362
;
data Month5;
input Name $ sales;
cards;
Joyce 209
Marsha 355
Bill 302
Vernon 416
Sally 475
;
data Month6;
input Name $ sales;
cards;
Joyce 306
Marsha 472
Bill 351
Vernon 405
Sally 358
;
options symbolgen;
%Macro quarter(quarter);
%Global qtr;
%Let qtr = &quarter;
%Mend quarter;
%quarter (1);
options mprint symbolgen;
%Macro Month(day);
%Global One;
%Global Two;
%Global Three;
%if &qtr = %eval(1) %then %do;
%Let One = 1;
%Let Two = 2;
%Let Three = 3;
%end;
%Else %if &qtr = %eval(2) %then %do;
%Let One = 4;
%Let Two = 5;
%Let Three = 6;
%end;
%Else %if &qtr = %eval(3) %then %do;
%Let One = 7;
%Let Two = 8;
%Let Three = 9;
%end;
%Else %if &qtr = %eval(4) %then %do;
%Let One = 10;
%Let Two = 11;
%Let Three = 12;
%end;
%Mend Month;
%Month(&qtr);
Correct code:
%Macro qtrearn(x);
proc sql;
create table qtr&x as
select Month&One..name, month&One..sales as m&One.sales, month&Two..sales as m&Two.sales,
month&Three..sales as m&Three.sales, sum(month&One..sales, month&Two..sales, month&Three..sales) as qtr&x.sales
from month&One, month&Two, month&Three
where month&One..name=month&Two..name=month&Three..name;
select sum(m&One.sales) as m&One.total, sum(m&Two.sales) as m&Two.total, sum(m&Three.sales) as m&Three.total,
sum(qtr&x.sales) as qtr&x.total
from qtr&x;
%Mend qtrearn;
%qtrearn(&qtr);
Code that is not working with the do loop. I need to insert the do loop for an assignment.
options mprint symbolgen;
%Macro qtrearn(x);
proc sql;
%do i = &One %to &Three;
create table qtr&x as
select Month&i..name, month&&i..sales as m&&i.sales,
sum(month&One..sales, month&Two..sales, Month&Three..sales) as qtr&x.sales
from month&One, month&Two, month&Three
where month&One..name=month&Two..name=month&Three..name;
%end;
select sum(m&One.sales) as m&One.total, sum(m&Two.sales) as m&Two.total, sum(m&Three.sales) as m&Three.total,
sum(qtr&x.sales) as qtr&x.total
from qtr&x;
%Mend qtrearn;
%qtrearn(&qtr);
I was able to solve it. In case someone else wants it. Here is the answer:
%Macro qtrearn(x);
proc sql;
create table qtr&x as
select Month&One..name,
%do i = &One %to &Three;
month&&i..sales as m&&i.sales,
%end;
sum(month&One..sales, month&Two..sales, month&Three..sales) as qtr&x.sales
from month&One, month&Two, month&Three
where month&One..name=month&Two..name=month&Three..name;
create table totals_qtr&x as
select %do i = &One %to &Three;
sum(m&&i.sales) as m&&i.total,
%end;
sum(qtr&x.sales) as qtr&x.total
from qtr&x;
proc print data=work.qtr&x;
run;
proc print data=work.totals_qtr&x;
run;
%Mend qtrearn;
%qtrearn(&qtr);

Execution of data step despite of the errors

I am executing the following code to combine a large number of data sets . Here is code:
%macro combine;
data modelfit;
set
%do i = 30 %to 116 %by 3 ;
stat&i
%end;
;
%end;
run;
%mend;
%combine;
However, few of the data sets are not present. Hence, the data set is failing to execute. How can I prevent the data step from stopping to execute and combine the data sets that are there . Can anybody please help me with this
Check if the files exist before adding them in the loop:
%macro combine;
data modelfit;
set
%do i = 30 %to 116 %by 3 ;
%if %sysfunc(exist(stat&i)) %then %do ; stat&i %end ;
%end;
;
run;
%mend;
%combine;