This question already has answers here:
Dynamically call macro from sas data step
(3 answers)
Closed 6 years ago.
I've got a macro which draws a pie chart for a provided ID. It basically chooses a row from a table, then transposes obtained one-row table and then draws a pie chart. It works perfectly fine if I call it for one observation (for ex. %StudPieChart(931123)). Here's the code:
%MACRO StudPieChart(id);
data projekt.temp;
set projekt.cwiczenia(keep=nrInd KOL1 KOL2 KOL3 aktywnosc where= (nrInd=&id));
drop nrInd;
run;
proc transpose data=projekt.temp out=projekt.temp;
run;
proc gchart data=projekt.temp;
pie _NAME_ / sumvar=COL1 percent=inside;
run;
%MEND;
Now I want draw a chart for not one, but some sample of observations. So I generated random sample and tried to run a macro in a data step. But it doesn't work anymore and I have no clue why.
Here's the rest of code:
proc surveyselect data=projekt.cwiczenia out=projekt.sample(keep=nrInd) sampsize=5 NOPRINT;
run;
data _NULL_;
set projekt.sample;
%StudPieChart(nrInd);
run;
You can use CALL EXECUTE.
data _NULL_;
set projekt.sample;
call execute('%nrstr(%StudPieChart('||nrInd||'));');
run;
RTM: http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1blnvlvciwgs9n0zcilud6d6ei9.htm
Related
I have a simple data table in SAS, where I have the results from a survey I sent to my friends:
DATA Questionnaire;
INPUT make $ Question_Score ;
CARDS;
Ned 1
Shadowmoon 2
Heisenberg 1
Athelstan 4
Arnold 5
;
RUN;
What I want to do, using SAS, is to export this table into SPSS (.sav), and also have the value labels for the Question_Score, like shown in the picture below:
I then proceed to create a format in SAS (in hope this would do it):
PROC FORMAT;
VALUE Question_Score_frmt
1="Totally Agree"
2="Agree"
3="Neutral"
4="Disagree"
5="Totally Disagree"
;
run;
PROC FREQ DATA=Questionnaire;
FORMAT Question_Score Question_Score_frmt.
;
TABLES Question_Score;
RUN;
and finally export the table to a .sav file using the fmtlib option:
proc export data=Questionnaire outfile="D:\Questionnaire.sav"
dbms=spss replace;
fmtlib=work.Q1frmt;
quit;
Only to disappoint myself seeing that it didn't work.
Any ideas on how to do this?
You didn't apply the format to the dataset, unfortunately, you applied it to the proc freq. You would need to use PROC DATASETS or a data step to apply it to the dataset.
proc datasets lib=work;
modify questionnaire;
format Question_Score Question_Score_frmt.;
run;
quit;
Then exporting will include the format, if it's compatible in SAS's opinion with SPSS's value label rules. I will note that SAS's understanding of SPSS's rules is quite old, based on I think SPSS version 9, and so it's fairly often that it won't work still, unfortunately.
I am new to SAS and need to sgplot 112 variables. The variable names are all very different and may change over time. How can I call each variable in the statement without having to list all of them?
Here is what I have done so far:
%macro graph(var);
proc sgplot data=monthly;
series x=date y=var;
title 'var';
run;
%mend;
%graph(gdp);
%graph(lbr);
The above code can be a pain since I have to list 112 %graph() lines and then change the names in the future as the variable names change.
Thanks for the help in advance.
List processing is the concept you need to deal with something like this. You can also use BY group processing or in the case of graphing Paneling in some cases to approach this issue.
Create a dataset from a source convenient to you that contains the list of variables. This could be an excel or text file, or it could be created from your data if there's a way to programmatically tell which variables you need.
Then you can use any of a number of methods to produce this:
proc sql;
select cats('%graph(',var,')')
into: graphlist separated by ' '
from yourdata;
quit;
&graphlist
For example.
In your case, you could also generate a vertical dataset with one row per variable, which might be easier to determine which variables are correct:
data citiwk;
set sashelp.citiwk;
var='COM';
val=WSPCA;
output;
var='UTI';
val=WSPUA;
output;
var='INDU';
val=WSPIA;
output;
val=WSPGLT;
var='GOV';
output;
keep val var date;
run;
proc sort data=citiwk;
by var date;
run;
proc sgplot data=citiwk;
by var;
series x=date y=val;
run;
While I hardcoded those four, you could easily create an array and use VNAME() to get the variable name or VLABEL() to get the variable label of each array element.
I want to make a plot between a variable and its observation number. From here http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_reg_sect017.htm
I see that the keyword is OBS.
so thus my code is
proc gplot data=my_data;
plot OBS.*my_v;
run;
by I still get an error. What's the proper way to do this?
Your reference refers to proc reg not proc gplot. You may need to add the observation number in to the data set.
data my_data;
set my_data;
obs=_n_;
run;
proc gplot data=my_data;
plot OBS*my_v;
run;
I am trying to determine the number of observations in a dataset, then convert this number into a macro variable that i can use as part of a loop. I've searched the web for answers and not had much luck. I would post some example code I've tried but I have literally no idea how to approach this.
Could anybody assist?
Thanks
Chris
SAS stores dataset information, such as number of observations, separately, so the key is to access this information without having to read in the entire dataset.
The following code will do just that, the if 0 part is never true so the dataset isn't read, however the information is.
data _null_;
if 0 then set sashelp.class nobs=n;
call symput('numobs',n);
stop;
run;
%put n=&numobs;
You can also get it from dictionary.tables like this:
proc sql noprint;
select nobs into :nobs
from dictionary.tables
where libname='YourLibrary' and memname='YourDatasetName';
quit;
Here it is:
Create macro variable:
data _null_;
set sashelp.class;
call symput("nbobs",_N_);
run;
See result:
%put &nbobs;
Use it:
data test;
do i = 1 to &nbobs;
put i;
end;
run;
I need to rerun the same program with data from different months and create a separate excel spreadsheet for each month. What is a shorter way to program this in SAS than to run each program separately? For example in the following I read data from October, and at the end of the same program I output the October results to excel. I need to do the same for each month. Can I do it in one SAS program (maybe using Macro)? Thanks.
data sourceh.trades2;
set sourceh.trades1_october08_wk1;
if time<34200000 or time>57602000 then delete;
run;
proc export data=sourceh.avesymbol
outfile='C:\Documents and Settings\zd\My Documents\h\hdata\trades\2008\October 08 1 min correlations.xls'
replace;
run;
I would use a macro for that. Here I have wrapped your code into a macro which you can execute with the RunProgram(); macro statement for each desired month and year.
%MACRO RunProgram(month, year);
data sourceh.trades2;
set sourceh.trades1_&month.&year._wk1;
if time<34200000 or time>57602000 then delete;
run;
proc export data=sourceh.avesymbol
outfile="C:\Documents and Settings\zd\My Documents\h\hdata\trades\2008\&month. &year. 1 min correlations.xls"
replace;
run;
%MEND RunProgram;
%RunProgram(October, 08);
%RunProgram(November, 08);