Programming the statistical procedures from SAS

Proc Means on a char variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Proc Means on a char variable


Hi Team,

I have a numeric variable alled Age and I used formats on it. It became charecter variable. Now I want to use that variable to find mean ,median etc?

I get an error..

Could you help me resolve this issue please

%macro age(dname1=,dname2=);

proc means data=&dname1 nway noprint;
var age;
output out=&dname1._out n= mean= std= median= min= max=/autoname;
run;

data statistics;
set &dname1._out;
keep n meansd median minmax;
n=put(age_N,4.);
meansd=put(age_Mean,4.1)|| " ("||put(age_StdDev,4.1)||" )";
median=put(age_Median,4.1);
minmax=put(age_Min,4.1)  ||"/"|| put(age_Max,4.1);
run;

proc transpose data=statistics out=&dname1._trans;
var n meansd median minmax;
run;

data pre_final(drop=_name_);
set &dname1._trans;
stat=put(_name_,$age.);
run;

data &dname2;
set age_label pre_final;
run;
%mend;

%age(dname1=grafts, dname2=grafts_final);

177  %age(dname1=grafts, dname2=grafts_final);
ERROR: Variable Age in list does not match type prescribed for this list.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.GRAFTS_OUT may be incomplete.  When this step was stopped there were 0
         observations and 0 variables.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      Memory                            143k
      OS Memory                         15472k
      Timestamp            10/4/2012  12:17:09 PM

NOTE: Variable age_N is uninitialized.
NOTE: Variable age_Mean is uninitialized.
NOTE: Variable age_StdDev is uninitialized.
NOTE: Variable age_Median is uninitialized.
NOTE: Variable age_Min is uninitialized.
NOTE: Variable age_Max is uninitialized.

Thanks


Accepted Solutions
Solution
‎10-04-2012 02:23 PM
Occasional Contributor
Posts: 14

Re: Proc Means on a char variable

Yep, because you put it into a character.  If you want to avoid that, use the format statement instead.  Then it will carry the format, but stay as a numeric variable.

View solution in original post


All Replies
Esteemed Advisor
Posts: 7,072

Re: Proc Means on a char variable

Karun,

Can you post an example of what you did?  Simply applying a format doesn't change a variable's type.

Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

I am sorry Arthur for the vague question. A put function was used on numeric age values to convert to  charecter.:

PROC FORMAT;

VALUE AGEFMT

0-40="1-<=40 Years"

41-60="2-41-60 Years"

61-high="3-61+ Years"

;

run;

I think i cannot use the proc Means on the age variable now.....

Solution
‎10-04-2012 02:23 PM
Occasional Contributor
Posts: 14

Re: Proc Means on a char variable

Yep, because you put it into a character.  If you want to avoid that, use the format statement instead.  Then it will carry the format, but stay as a numeric variable.

Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

Hi Arthur,

Could you please explain me the code for the "Do Loop" title I posted.......if you have some time?

Thanks

Grand Advisor
Posts: 16,905

Re: Proc Means on a char variable

I'm attaching a macro I used when working on clinical trials to generate the table of characteristics, as it seems like that's what you're doing.

Hopefully it might help you a bit. It's based on the premise of apply formats (not with put) to your variables as required. If you need a summary of a continuous and categorical variable simply include it in both parameter lists. The continuous portion will not use the format while the categorical will. Also if you label your variables the labels will be added in to the summary rather than the variable name.

Attachment
Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

Thanks Reeza,

It helps a lot. But a few questions as usual

1)I can understand it a little bit but when I go from top to bottom I am lost.

2)Could you please explain me superficially if you have some time?

I know its difficult for you guys to type a lot of info..But I am getting better day by day...

Thanks

Grand Advisor
Posts: 16,905

Re: Proc Means on a char variable

I won't go through line by line...2 reasons:

1) no time

2) its a useful exercise trying to 'read' someone else's code and refer to documentation to find answers. Then you can learn faster rather than waiting on responses from here, though it is useful. You'll also learn how to change things without having to ask every time. 

For example here is the documentation for call symput.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000127861.htm

And a paper that is way more detailed:

http://www2.sas.com/proceedings/sugi29/052-29.pdf

There is no %let call symput.

Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

Thanks so very much. This code is very useful . I was analysing it until now. I understand the time factor. I will go through the documentation you provided.

Once again I appreciate your time

Cheers

Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

Hi ,

I have done some reading last night and is convinced with the attached code.

I have a question though regarding looping through the variable list

Could you please tell me the meaning of :

%do %while (%scan(&cat, &i, " ") ^=%str());

and why are we using i=1??

My understanding:

Scan extracts words and we are telling to extract variable from the macro variable "cat"  seperated by a space

concern:why only the first variable of the list?

Also what does ^=%str() mean??

The following code has been cut short from the original attachment

%macro table_char(dsetin, cont, cat, dsetout);

*loop through variable list;
%let i=1;
%do %while (%scan(&cat, &i, " ") ^=%str());
  %let var=%scan(&cat, &i, " ");


Get format for variable;
data _null_;
  set &dsetin;
   call symput("var_fmt", vformat(&var));
run;


proc freq data=&dsetin noprint;
table &var/missing out=tab_var;
run;

%mend

%table_char(sample, height weight age, sex age, sample_table_char);

Occasional Contributor
Posts: 14

Re: Proc Means on a char variable

Hi Karun, here are my 2 cents worth on your points of confusion.  I understand what it's like to be looking at macro code at first, so I will try to shed some light on some of it for you (although it really is best ultimately to just dive in and start to play with the code and you will start to slowly get it).

Could you please tell me the meaning of :

%do %while (%scan(&cat, &i, " ") ^=%str());

What this is doing is setting up a do while loop on a list contained in a macro variable called &CAT.  When it hits a blank spot in the string, it will stop do while-ing.

%str() is a fancy macro way of just saying =; In other words it's searching for an empty string to halt the do while.  So when it hits the spot there are no more CATs, it stops.

and why are we using i=1??

Well somebody is %let - ting this occur.  Maybe they just wanted the first one that time.  In order to get through the whole thing you need to let &i increment one by one, and hence step through the list.

I hope these few thoughts help you.


Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

Great help,

It makes me understand the concepts better now.

Super Contributor
Posts: 1,040

Re: Proc Means on a char variable

I would also be happy to know whats going on with the %let call symput statements!!

Thanks for your time

Regards

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 1319 views
  • 6 likes
  • 4 in conversation