Programming the statistical procedures from SAS

FORMATS QUESTION

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

FORMATS QUESTION


Hi Team,

I have several datasets after proc freqs:

attack_freqs

attack       count    percent

1-heart-      40         40

2-heart+     60         60

stroke_freqs

stroke       count    percent

1-stroke-      50         50

2-stroke+     50         50

pain_freqs

pain       count    percent

1-pain-      50         50

2-pain+    50         50

These variables(attack stroke and pain have been formatted in the dataset prior to computing freqs.

When I try to set them finally (after changing the variable names to a common variable name(eg. PARAMETER).......";

data final;

set attack_freqs(in=in1) stroke_freqs(in=in2) pain_freqs(in=in3);

run;

Unfortunately the result lookslike this:

Parameter       count    percent

1-heart-             40         40

2-heart+           60         60

1-heart-           50         50

2-heart+           50         50

1-heart-         50         50

2-heart+          50         50

STROKE AND PAIN VANISHES NOW;

HEART FORMAT HAS BEEN RETAINED FOR ALL THE OTHER VARIABLES TOOO

I WANT:

Parameter       count    percent

1-heart-             40         40

2-heart+           60         60

1-stroke-           50         50

2-stroke+           50         50

1-pain-           50         50

2-pain+             50        50

THANKS


Accepted Solutions
Solution
‎09-26-2012 03:20 PM
Super User
Posts: 18,491

Re: FORMATS QUESTION

My guess.

When you run a proc freq with a labelled variable it produces a table with the underlying variable and applies the format. If you look at the table for example you'll see a format attached to the variable of interest.

My guess is when you combined the tables that one format was set for the variable, heart. You can check the column of the old table and see if this was the case.

View solution in original post


All Replies
Super User
Posts: 18,491

Re: FORMATS QUESTION

Post the code you used if you'd like help debugging.

Otherwise an appropriate solution would be:

data final;

set attack_freqs(rename=attack=parameter) stroke_freqs(rename=stroke=parameter) pain_freqs(rename=pain=parameter);

run;

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

Hi Reeza,

Did you find any errors in the code so far??

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

Hi Team,

I got the answer at last. I went ahead to change the following in the first datastep

FORMAT CmbDbt $DIA.;

FORMAT CmbSmk1 $SMOK.;

FORMAT CmbCAD $CORONARY.;

FORMAT CmbCncx $CANCER.;

FORMAT CmbHTN $HTN.;

FORMAT CmbCOPD $PULMONARY

     TO

Cmbdt1=put(CmbDbt,$DIA.);

CmbSmk2=put(CmbSmk1,$SMOK.);

CmbCAD1=put(CmbCAD,$CORONARY.);

CmbCncx1=put(CmbCncx,$CANCER.);

CmbHTN1=put(CmbHTN ,$HTN.);

CmbCOPD1=put(CmbCOPD ,$PULMONARY.);

IT WORKED FINE. COULD SOMEONE EXPLAIN ME WHY IS THAT I AM NOT GETTING THE ANSWER IF I DIRECTLY APPLY THE FORMAT ON THE VARIABLE?????

THANKS

Super User
Super User
Posts: 6,681

Re: FORMATS QUESTION

I didn't read all of the code you posted but from your desired output it looks like you wanted different format attached to different observations of the same variable. There is not anyway to do that. By creating the decoded values as a character string using the PUT() function you can put the values into the a single variable.

I WANT:

Parameter       count    percent

1-heart-           40         40

2-heart+           60         60

1-stroke-          50         50

2-stroke+          50         50

1-pain-            50         50

2-pain+            50         50

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

Hi Tom,

The question was different.  The dataset you see if after combing 3 different datasets which has 3 diff formats appiled(one to each dataset)  Thanks though.

Super User
Super User
Posts: 6,681

Re: FORMATS QUESTION

Which you cannot do. Once the datasets are combined there is only one variable which can have only one format attached to it.

Try the little program below to demonstrate.

proc format;

value one 1='One';

value two 1='Two';

run;

data one ;

  code=1;

  format code one.;

  decode=put(code,one.);

  put code 1. +1 code= decode= ;

run;

data two ;

  code=1;

  format code two.;

  decode=put(code,two.);

  put code 1. +1 code= decode= ;

run;

data both;

  set one two;

  put code 1. +1 code= decode= ;

run;

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION


Perfect example Tom,

Thanks for your time.

In the place of Two we are getting One in your example

while we are setting the two datasets because it is carrying the format with it even to the next?????

Am i right??????

Setting datasets with values which have two different formats under the same variable: The first prevails....AM I RIGHT TOM??

Super User
Super User
Posts: 6,681

Re: FORMATS QUESTION

I believe that it will use the format from the first dataset that has a format for that variable.  So if the first dataset did not attach a format to the variable but the second one did then the format defined in the second dataset is used.

You can override with a FORMAT statement in the data step.  If you have multiple FORMAT statements then the LAST one wins.

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

code variable  in the first dataset(one) has a format  ONE.----------------------value is 'ONE'

code variable in the second dataset (two ) has a format TWO.----------------value is 'TWO'

when we set the two datasets the variable code has values ONE and ONE now

When the variable name is similar It is using format from the first dataset .......that is why 1 gets a value of ONE instead of TWO!!!!

This time Tom?DID i get it right??

Thanks

Super User
Super User
Posts: 6,681

Re: FORMATS QUESTION

Close.  The variable CODE in the example is numeric. All of the values of CODE are the number 1.

In the dataset ONE the variable DECODE is character add gets the value 'ONE'.  Because CODE has the format ONE. attached the 1 in it is displayed as 'ONE'  Whereas in the dataset TWO we are using format TWO. so even though the value of CODE  is still 1  it displays as 'TWO' and the variable DECODE gets a value of 'TWO'.

In the third dataset the variable CODE gets the format ONE. attached so both rows will display the 1 in the variable CODE as 'ONE'.  But since the value of character variable DECODE was already defined earlier it will still say 'ONE' on the first observation and 'TWO' on the second.

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

Thanks for your time.

Unless the expert goes into details of even easy looking codes(like this one).....its difficult to get going  for beginners like us.

Great Help

Solution
‎09-26-2012 03:20 PM
Super User
Posts: 18,491

Re: FORMATS QUESTION

My guess.

When you run a proc freq with a labelled variable it produces a table with the underlying variable and applies the format. If you look at the table for example you'll see a format attached to the variable of interest.

My guess is when you combined the tables that one format was set for the variable, heart. You can check the column of the old table and see if this was the case.

Super Contributor
Posts: 1,040

Re: FORMATS QUESTION

I agree with you Reeza unless someone questions it...

Also I

Regards

Thanks a ton

Super User
Posts: 18,491

Re: FORMATS QUESTION

Can i suggest a proc tabulate procedure instead, and then summarized with a datastep?

ods output table=table1;

proc tabulate data=demographics;

    class age gender bmi carpini optime asa CmbDbt CmbSmk1 CmbCAD CmbCncx CmbHTN CmbCOPD Failures;

    table age gender bmi carpini optime asa CmbDbt CmbSmk1 CmbCAD CmbCncx CmbHTN CmbCOPD Failures, n colpctn;

run;

data summary_demo;

    set table1;

    array vars(*) age gender bmi carpini optime asa CmbDbt CmbSmk1 CmbCAD CmbCncx CmbHTN CmbCOPD Failures;

    Variable=cats("",of vars(*));

    Name=vname(vars(whichc(variable, of vars(*))));

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 16 replies
  • 337 views
  • 6 likes
  • 3 in conversation