Prev: Proc Import Excel Range
Next: Data step Question
From: Mieds on 17 Mar 2010 15:18 Hi, I'm new to SAS and would like to write a macro that loops through a number of times and can set two variables based on an if statement. I am wondering if this is possible, or if I have to use two if statements, one for each variable. Thanks in advance for any suggestions. For example, %MACRO CreateTables (CatNums); %do n=1 %to &CatNums; %if &n=1 %then %let catname=One and %let catnumber=1; %else %if &n=2 %then %let catname=Two and %let catnumber=2; PROC SQL; CREATE TABLE Category&CatName AS SELECT * FROM Data AS T1 WHERE T1.category = &CatNumber QUIT; %end; %MEND CreateTables;
From: Bachenot2 on 17 Mar 2010 18:27 On Mar 17, 12:18 pm, Mieds <mattm...(a)yahoo.com> wrote: > Hi, > > I'm new to SAS and would like to write a macro that loops through a > number of times and can set two variables based on an if statement. I > am wondering if this is possible, or if I have to use two if > statements, one for each variable. > > Thanks in advance for any suggestions. > > For example, > > %MACRO CreateTables (CatNums); > > %do n=1 %to &CatNums; > %if &n=1 %then %let catname=One and %let catnumber=1; > %else %if &n=2 %then %let catname=Two and %let catnumber=2; > > PROC SQL; > CREATE TABLE Category&CatName AS > SELECT * > FROM Data AS T1 > WHERE T1.category = &CatNumber > QUIT; > > %end; > %MEND CreateTables; If you are trying to assign values of '1' the value of 'One' and so on in a datasets, this code will work. The data used is the commonly-used sample dataset 'Clinics'. proc sql; create table step1 as select *, case when race = '1' then 'One' when race = '2' then 'Two' else 'Oth' end as racecat from sasdata.clinics; quit; proc sql; select distinct racecat, count(*) as rows from step1 group by racecat; quit; Results: racecat rows ----------------- One 44 Oth 18 Two 18
From: Steve James on 18 Mar 2010 11:48 You probably don't need a macro for this problem as you are probably better off keeping everything in one dataset and using BY processing. But to answer your immediate question, you can use the %do..%end construct to do what you want. E.G. %MACRO CreateTables (CatNums); %do n=1 %to &CatNums; %if &n=1 %then %let catname=One and %let catnumber=1; %else %if &n=2 %then %let catname=Two and %let catnumber=2; PROC SQL; CREATE TABLE Category&CatName AS SELECT * FROM Data AS T1 WHERE T1.category = &CatNumber QUIT; %end; %MEND CreateTables; On Mar 17, 3:18 pm, Mieds <mattm...(a)yahoo.com> wrote: > Hi, > > I'm new to SAS and would like to write a macro that loops through a > number of times and can set two variables based on an if statement. I > am wondering if this is possible, or if I have to use two if > statements, one for each variable. > > Thanks in advance for any suggestions. > > For example, > > %MACRO CreateTables (CatNums); > > %do n=1 %to &CatNums; > %if &n=1 %then %let catname=One and %let catnumber=1; > %else %if &n=2 %then %let catname=Two and %let catnumber=2; > > PROC SQL; > CREATE TABLE Category&CatName AS > SELECT * > FROM Data AS T1 > WHERE T1.category = &CatNumber > QUIT; > > %end; > %MEND CreateTables;
From: Steve James on 18 Mar 2010 11:56 This may be a duplicate as I inadvertently hit enter before i was done. Sorry. You probably don't need a macro here and would be better off using BY- Group processing on a single dataset. Bachenot2 has given you a solution towards that end. However to answer your immediate question you can use %do and %end to accomplish what you want. Example %MACRO CreateTables (CatNums); %do n=1 %to &CatNums; %if &n=1 %then %do ; %let catname=One ; %let catnumber=1; %end ; %else %if &n=2 %then %do ; %let catname=Two ; %let catnumber=2; %end ; PROC SQL; CREATE TABLE Category&CatName AS SELECT * FROM Data AS T1 WHERE T1.category = &CatNumber QUIT; %end; %MEND CreateTables; Steve On Mar 17, 3:18 pm, Mieds <mattm...(a)yahoo.com> wrote: > Hi, > > I'm new to SAS and would like to write a macro that loops through a > number of times and can set two variables based on an if statement. I > am wondering if this is possible, or if I have to use two if > statements, one for each variable. > > Thanks in advance for any suggestions. > > For example, > > %MACRO CreateTables (CatNums); > > %do n=1 %to &CatNums; > %if &n=1 %then %let catname=One and %let catnumber=1; > %else %if &n=2 %then %let catname=Two and %let catnumber=2; > > PROC SQL; > CREATE TABLE Category&CatName AS > SELECT * > FROM Data AS T1 > WHERE T1.category = &CatNumber > QUIT; > > %end; > %MEND CreateTables;
From: Mieds on 18 Mar 2010 13:53 Thanks Steve, That's what I was trying to do. On Mar 18, 10:56 am, Steve James <spj...(a)gmail.com> wrote: > This may be a duplicate as I inadvertently hit enter before i was > done. Sorry. > > You probably don't need a macro here and would be better off using BY- > Group processing on a single dataset. Bachenot2 has given you a > solution towards that end. However to answer your immediate question > you can use %do and %end to accomplish what you want. Example > > %MACRO CreateTables (CatNums); > > %do n=1 %to &CatNums; > > %if &n=1 > %then %do ; > %let catname=One ; > %let catnumber=1; > %end ; > %else > %if &n=2 > %then %do ; > %let catname=Two ; > %let catnumber=2; > %end ; > > PROC SQL; > CREATE TABLE Category&CatName AS > SELECT * > FROM Data AS T1 > WHERE T1.category = &CatNumber > QUIT; > %end; > %MEND CreateTables; > > Steve > > On Mar 17, 3:18 pm, Mieds <mattm...(a)yahoo.com> wrote: > > > Hi, > > > I'm new to SAS and would like to write a macro that loops through a > > number of times and can set two variables based on an if statement. I > > am wondering if this is possible, or if I have to use two if > > statements, one for each variable. > > > Thanks in advance for any suggestions. > > > For example, > > > %MACRO CreateTables (CatNums); > > > %do n=1 %to &CatNums; > > %if &n=1 %then %let catname=One and %let catnumber=1; > > %else %if &n=2 %then %let catname=Two and %let catnumber=2; > > > PROC SQL; > > CREATE TABLE Category&CatName AS > > SELECT * > > FROM Data AS T1 > > WHERE T1.category = &CatNumber > > QUIT; > > > %end; > > %MEND CreateTables;
|
Pages: 1 Prev: Proc Import Excel Range Next: Data step Question |