General Question-formats

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

General Question-formats

Hi Team,

I have a dataset with several (maybe 35) variables(numeric as well as char variables).

char variables  have Y or N

numeric has numbers like age, BMI etc

i want to create and apply those formats on all of these 35 variables

Example of one variable shown

AIDS         AIDS

Y              AIDS+

N              AIDS-

I want to use these variables.

After done with the above step I want to put all these 35 variables in a macro where all continuous variables go as parameters under CONT

and all categorical variables go as parameters under  CAT

QUESTION:

How do i apply the formats for these 35 variables. Which of these two methods is good to go for????

z=put(AIDS,char.);

format AIDSchar.;


Accepted Solutions
Solution
‎10-17-2012 04:57 PM
Super User
Super User
Posts: 6,500

Re: General Question-formats

The syntax for the FORMAT statement is lists of variables followed by a format name.

FORMAT var1 var3 var5 $aids. var2 F5.2 var6 var7 dollar7.2 ;

You can also use ATTRIB statement in a data step to assign formats.

The code that Lin Lin has posted is a way to use formats create new variables with the decoded values of the old variables.  That is different operation than what is normally meant by "formatting" a variable in SAS.  If that is what you are interested in then you might want to look at the function VVALUEX to get the formatted value of a variable.

For example to create a vertical structure with variable name and formatted value you could do:

data want;

  set have;

  format var1 var3 var5 $aids. var2 F5.2 var6 var7 dollar7.2 ;

  do varname='var1','var2','var3','var4','var5','var6','var7';

     value = vvaluex(varname);

     output;

  end;

  keep idvar varname value ;

run;

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: General Question-formats

If you only want to apply a format to a variable, then you don't need to use PUT function. PUT function converts a numeric variable to a character variable.

Super Contributor
Posts: 1,040

Re: General Question-formats

Thanks. I was confused

Regards

Respected Advisor
Posts: 3,124

Re: General Question-formats

I can't picture an easy way doing it using format (both creating them and applying them). but if you don't mind creating new variables, array() maybe meet your need:

data have;

input id ( AIDS HIV) (:$8.);

cards;

1 Y              Y

1 N              Y

;

data want;

set have;

   array c _character_;

   array c_new $ AIDS_new HIV_new;

   do i=1 to dim(c);

     if c(i)='Y' then c_new(i)=cats(vname(c(i)),'+');

       else if c(i)='N' then c_new(i)=cats(vname(c(i)),'-');

       end;

       drop i;

run;

proc print;run;

    

Haikuo

Super Contributor
Posts: 1,040

Re: General Question-formats

Hai,

I have a couple of questions in your code...

1)Your code can be helpful when therwe are several variables and each variable has to be assigned the SAME format?

My next question would be "how can we apply diiferent variables to different formats at the same time??

2)

array c _character_;

   array c_new $ AIDS_new HIV_new;

why are there 2 array statements?

in the second array statement is c_new an ARRAY name????

3)your array explains only charecter variables . How to proceed if i have mixed variables?? Tom explained about it a little bit so I wana Incorporate that point as well in your code

??

Regards

Super Contributor
Posts: 1,636

Re: General Question-formats

Yes. c_new is an array.  AIDS_new, HIV_new are two new variables just created

Super Contributor
Posts: 1,040

Re: General Question-formats

Thanks. But what is the first array statement doing?what is the neccisity to define two array stmnts?

I see that the first array is considering all char variables....cant that be incorporated in the second statement somehow??

Thanks

Respected Advisor
Posts: 3,124

Re: General Question-formats

The first array is to define the variables coming with the dataset, it is not including the newly created variables. And if you want to do the same for numeric variables, use: array array_name _numeric_; or there are some other definitions that using variable positions (v1--v20) or variable initials (v1-v20) to include multiple variables in array.

Haikuo

Super Contributor
Posts: 1,040

Re: General Question-formats

you mean in the same array use another array stmnt for numeric?

or in a different dataset altogether?

Super Contributor
Posts: 1,636

Re: General Question-formats

No.

first array is for the the variables already in the dataset. second array is for the new variables going to be created.

Haikuo's code is the same as the code below. It makes your life easy if you have a lot of variables.

data have;

input id ( AIDS HIV) (:$8.);

cards;

1 Y              Y

1 N              Y

;

data want;

set have;

if AIDS='Y' then AIDS_new="AIDS+";

   else AIDS_new="AIDS-";

if HIV='Y' then HIV_new="HIVS+";

   else HIV_new="HIV-";

run;

proc print;run;

Super Contributor
Posts: 1,040

Re: General Question-formats

Hi,

Thanks for the detailed explanation.

So Hai's method is for applying different formats to different variables  Am i RIGHT??? (i thought it for applying the same format to different variables at the first glance)...

Secondly,

Do I need to write the _new for 50 variables if 50 of them are present for which i need to apply formats????

Thanks

Super User
Posts: 17,840

Re: General Question-formats

NO. Haiku method RECODES the variables.

To APPLY a format use Tom's code in a datastep.

FORMAT var1 var3 var5 $aids. var2 F5.2 var6 var7 dollar7.2 ;

Super Contributor
Posts: 1,040

Re: General Question-formats

Great Help all of you for making me understand the concept. I will have to go through each of your answers point by point and write down the key points in my book. Thanks each and everyone in person for the quick responses... Team is very helpful.

Sorry for my silly questions.

Regards

Super User
Posts: 17,840

Re: General Question-formats

Try following the code below. It might help explain sas formats a bit better. Take a good look at the proc contents and the variable type/formats as well.

And for the proc freq look at the variable vs the output.

*create your format here;

proc format;

    value teen_fmt

    low - 12 ='Pre Teen'

    13 - 19 = 'Teenager'

    19 - high='Adult';

run;

data class;

    set sashelp.class;

    age_formatted=age;

    format age_formatted teen_fmt.;

    age_recode=put(age, teen_fmt.);

run;

proc print data=class;

run;

proc contents data=class;

run;

proc freq data=class;

    table age age_formatted;

    format age teen_fmt.;

    format age_formatted;

run;

Super Contributor
Posts: 1,040

Re: General Question-formats

Hi,

Thank you so much. I understood the example very good. I was going to ask you about:

proc freq data=class;

    table age age_formatted;

    format age teen_fmt.;

    format age_formatted;

run;

There was no format added to the variable age_formatted?

☑ This topic is SOLVED.

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

Discussion stats
  • 22 replies
  • 455 views
  • 13 likes
  • 6 in conversation