BookmarkSubscribeRSS Feed
Amoljadhav123
Calcite | Level 5

Hello Team,

 

I am a new SAS learner and have taken the initiative to learn SAS. Could you please give me an idea about the below SAS code? What exactly is this code doing ? what will be achieved thought this code?

 

%macro pull_rpad(start_date, end_date, data_num);
proc sql;
connect to teradata ( tdpid=dwprod user=&tduser. pass=&tdpwd.);
create table Exchange_&data_num. as
select * from connection to teradata
(select * from table

);
run;

 

proc SQL;
select (strip(name)) into :vars separated by '|' from vars
quit;

data _null_;
set Exchange_&data_num.;

file "/prd/sau20/Dave/Exchange_&data_num..txt" dsd dlm='|';
if _n_ = 1 then put "&vars";

%let dsid = %sysfunc(open(vars));
%let nobs = %sysfunc(attrn(&dsid,nlobs));


%do i=1 %to &nobs;
%let rc = %sysfunc(fetchobs(&dsid,&i));
%let vName = %sysfunc(getvarc(&dsid,1));
%let vType = %sysfunc(getvarn(&dsid,2));
%put &=vName &=vType;

%if &vType = 2 %then %do;
put (&vName) (~) @;
%end;
%else %if &vType = 1 %then %do;
put &vName @;
%end;

%if &i = &nobs %then %do;
put ;
%end;

%end;

run;

%let dsid = %sysfunc(close(&dsid));

%mend ;

%macro run_loop();
%let begin_date = '01SEP2017'd;
%let ii = 0;
%do ii = 0 %to 30;
data a;
in1 = put(intnx('month',&begin_date,&ii,'b'),yymmdd10.);
in2 = put(intnx('month',&begin_date,&ii,'e'),yymmdd10.);
in3 = year(intnx('month',&begin_date,&ii,'b'))*100+month(intnx('month',&begin_date,&ii,'b'));
call symput('in1',in1);
call symput('in2',in2);
call symput('in3',in3);
run;
%pull_rpad (&in1,&in2,&in3);
%end;
%mend;
%run_loop();

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Honestly, I think this code is useless gibberish.

 

It defines a macro named PULL_RPAD that has three arguments: start_date, end_date, data_num. The arguments start_date and end_date don't appear to be used anywhere, which is why I say it is gibberish.

 

Then macro run_loop is created which loops through months which creates values of in1 in2 in3 that are used as arguments to PULL_RPAD, and in1 and in2 are ignored by PULL_RPAD.

 

Of course, the code could be modified to do something useful, but I think I'll stop there.

--
Paige Miller
Tom
Super User Tom
Super User

It seems to be generating pipe delimited text files.

The main complexity seems to be around figuring out which variables are character so it can decide whether or not to use the ~ modifier on the PUT statement to force SAS to add quotes around values that do not need to be quoted. 

 

You should ask yourself if you really need the extra quotes.  You might even look at PROC EXPORT or ODS CSV to see if they already add the extra quotes.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 948 views
  • 2 likes
  • 3 in conversation