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

Hello everyone, 

 

I am learning how to get and organize some online information by using SAS macro, and need some help. Thank you very much in advance.

 

Question 1: How to generate links which include two parts (1) & (2)?

In part (2), "???" refers to more than 10,000 usernames saved in another other dataset named work. reviewers.  The "..." is codes for grabing data wanted and has been tested for grabing information in individual page already (so I omitted it here). The ideal outcome is that SAS goes into the profile of each reviewer's page and combine their information in one file.  I have no idea how to incorporate work. reviewers into this macro and make it work......

 

(1) %let urlbegin=https://www.websites.com/name=;

(2) %let urlend=???;

%let savename= test;

%macro reviewers(firstcounter,pages,site,reviewers,run);
%let counter=&firstcounter;
%do %until (&counter>&pages);
filename indata url "&urlbegin&counter&urlend";

......

 

%let counter=%eval(&counter+1);
%end;
%mend reviewers;

%allbags(1,1,reviews,test,1to1);

 

Question 2: How to sum up records?

Suppose dataset work.fees contains several records of the same reviewer regarding the membership fees he/she paid in the past 5 years, how could I create a file which just has the name of the reviewer and the total amount they paid? Some reviewers are new, so they may have only one record; while some are senior and have more than one records......

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Without specific examples of the code you are attempting to generate I am not going to guess at your requirements.

Basically what call execute does is built likes of SAS code to submit to the SAS system.

 

A crude example

data _null_;
   call execute ('Proc print data=sashelp.class;');
   call execute ('var name sex age;');
   call exectue ('run;');
run;

If you have data set with variables containin names of datasets and  variables to print then you can do:

 

data control;
   infile datalines dlm=',' truncover;
   informat ds $40. varlist $100.;
   input  ds varlist;
datalines;
sashelp.class,name sex
sashelp.cars,make model type
;
run;
data _null_;
   set control;
   length str $ 150;
   str= catx(' ','proc print data=',ds,';');
   call execute (str);
   str= catx(' ','var',varlist,';');
   call execute (str);
   call execute ('run;');
run;

Note the str values insert the data set name or variable list into a string containing the syntax which is submitted to the program processor.

 

The log from the above statemetns generates something like:

NOTE: CALL EXECUTE generated line.
1   + proc print data= sashelp.class ;
2   + var name sex ;
3   + run;

NOTE: Writing HTML Body file: sashtml5.htm
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds


4   + proc print data= sashelp.cars ;
5   + var make model type ;
6   + run;

 

Which you can see are simple proc print statements. The Note indicates that the lines with + at the begining of the lines were written by the Call Execute statement.

View solution in original post

4 REPLIES 4
ballardw
Super User

I) if you have data in a data step then perhaps Call excute would be the best way to generate the code. The filename statemetn and the ... code can be generated for each record in the data set without delving into macro loops .

Since you don't show anything related to variables in a data set I hesitate to try to create a viable Filename statement.

Any line of code that you can make as a character variable can be sent to the code processor with the Call Execute statement inside a datastep. Even a macro call but that is one time you do not want to use double quotes with macros.

 

 

 

 

2) the easiest for me would be something like:

 

Proc summary data=have nway;

    class reviewerid;

    var feespaid;

    output out=wantedsummary( drop=_type_) sum=;

run;

The output data set wanted summary would have the reviewer id variable, the sum of the feespaid variable and an additional variable _n_ that would indicate how many values of feespaid (records) were used to calculate the sum.

may0423
Obsidian | Level 7

Hi @ballardw,

 

Thank you very much for the details. But I don't quite understand the first question. Would you please give me more details of the statement? The related variables in question one is phonenum, address, email and comments.

 

Thank you again!

ballardw
Super User

Without specific examples of the code you are attempting to generate I am not going to guess at your requirements.

Basically what call execute does is built likes of SAS code to submit to the SAS system.

 

A crude example

data _null_;
   call execute ('Proc print data=sashelp.class;');
   call execute ('var name sex age;');
   call exectue ('run;');
run;

If you have data set with variables containin names of datasets and  variables to print then you can do:

 

data control;
   infile datalines dlm=',' truncover;
   informat ds $40. varlist $100.;
   input  ds varlist;
datalines;
sashelp.class,name sex
sashelp.cars,make model type
;
run;
data _null_;
   set control;
   length str $ 150;
   str= catx(' ','proc print data=',ds,';');
   call execute (str);
   str= catx(' ','var',varlist,';');
   call execute (str);
   call execute ('run;');
run;

Note the str values insert the data set name or variable list into a string containing the syntax which is submitted to the program processor.

 

The log from the above statemetns generates something like:

NOTE: CALL EXECUTE generated line.
1   + proc print data= sashelp.class ;
2   + var name sex ;
3   + run;

NOTE: Writing HTML Body file: sashtml5.htm
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds


4   + proc print data= sashelp.cars ;
5   + var make model type ;
6   + run;

 

Which you can see are simple proc print statements. The Note indicates that the lines with + at the begining of the lines were written by the Call Execute statement.

may0423
Obsidian | Level 7

Thank you so much @ballardw!!

 

I will play around with your codes.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 761 views
  • 1 like
  • 2 in conversation