BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8


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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

16 REPLIES 16
Reeza
Super User

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;

robertrao
Quartz | Level 8

Hi Reeza,

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

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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

robertrao
Quartz | Level 8

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.

Tom
Super User Tom
Super User

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;

robertrao
Quartz | Level 8


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??

Tom
Super User Tom
Super User

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.

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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.

robertrao
Quartz | Level 8

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

Reeza
Super User

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.

robertrao
Quartz | Level 8

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

Also I

Regards

Thanks a ton

Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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