Create new variables from format values

Oligg

What i want to do: I need to create a new variables for each value labels of a variable and do some recoding. I have all the value labels output from a SPSS file (see sample).

Sample:

proc format; library = library ;
   value SEXF
      1 = 'Homme'  
      2 = 'Femme' ;
   value FUMERT1F
      0 = 'Non'  
      1 = 'Oui , occasionnellement'  
      2 = 'Oui , régulièrement'  
      3 = 'Non mais j''ai déjà fumé' ;
   value ... (many more with different amount of levels)

The new variable name would be the actual one without F and with underscore+level (example: FUMERT1F level 0 would become FUMERT1_0).

After that i need to recode the variables on this pattern:

data ds; set ds;
    FUMERT1_0=0;
    if FUMERT1=0 then FUMERT1_0=1;

    FUMERT1_1=0;
    if FUMERT1=1 then FUMERT1_1=1;

    FUMERT1_2=0;
    if FUMERT1=2 then FUMERT1_2=1;

    FUMERT1_3=0;
    if FUMERT1=3 then FUMERT1_3=1;
run;

Any help will be appreciated :)

EDIT: Both answers from Joe and the one of data_null_ are working but stackoverflow won't let me pin more than one right answer.

data _null_

Update to add an _ underscore to the end of each name. It looks like there is not option for PROC TRANSREG to put an underscore between the variable name and the value of the class variable so we can just do a temporary rename. Create rename name=newname pairs to rename class variable to end in underscore and to rename them back. CAT functions and SQL into macro variables.

data have;
   call streaminit(1234);
   do caseID = 1 to 1e4;
      fumert1 = rand('table',.2,.2,.2) - 1;
      sex = first(substrn('MF',rand('table',.5),1));
      output;
      end;
   stop;
   run;
%let class=sex fumert1;
proc transpose data=have(obs=0) out=vnames;
   var &class;
   run;
proc print;
   run;
proc sql noprint;
   select catx('=',_name_,cats(_name_,'_')), catx('=',cats(_name_,'_'),_name_), cats(_name_,'_')
      into :rename1 separated by ' ', :rename2 separated by ' ', :class2 separated by ' '
      from vnames;
   quit;
%put NOTE: &=rename1;
%put NOTE: &=rename2;
%put NOTE: &=class2;
proc transreg data=have(rename=(&rename1));
   model class(&class2 / zero=none);
   id caseid;
   output out=design(drop=_: inter: rename=(&rename2)) design;
   run;
%put NOTE: _TRGIND(&_trgindn)=&_trgind;

enter image description here


First try: Looking at the code you supplied and the output from Joe's I don't really understand the need for the formats. It looks to me like you just want to create dummies for a list of class variables. That can be done with TRANSREG.

data have;
   call streaminit(1234);
   do caseID = 1 to 1e4;
      fumert1 = rand('table',.2,.2,.2) - 1;
      sex = first(substrn('MF',rand('table',.5),1));
      output;
      end;
   stop;
   run;

proc transreg data=have;
   model class(sex fumert1 / zero=none);
   id caseid;
   output out=design(drop=_: inter:) design;
   run;
proc contents;
   run;
proc print data=design(obs=40);
   run;

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a new column based on values from other variables

From Dev

Create new variables based upon specific values

From Dev

Create 2 new variables conditional on retaining values from previous rows using R

From Dev

How to create a new variable with values from different variables if another variable equals a set value in R?

From Dev

How to create a new variable with values from different variables if another variable equals a set value in R?

From Dev

jQuery: create variables from min and max values?

From Dev

Create new variables with lag data from all current variables

From Dev

Create a new color from two RGB values

From Dev

Create a new color from two RGB values

From Dev

Create new array with values from other array

From Dev

create new dataframe from missing values

From Dev

Get associative array values into variables,which is in string format from cookies

From Dev

Extracting multiple values from variable and assigning new variables

From Dev

Create a new list with values from fields from existing list

From Dev

From list create new one list with explicity values from first

From Dev

Make dummy variables from multiple columns and create new dataframe

From Dev

Create a new variable from existing variables efficiently in python

From Dev

Make dummy variables from multiple columns and create new dataframe

From Dev

How to create categorical variables from text values in R

From Dev

Create dynamic object from string, with variables as anchor property values

From Dev

Laravel: How to create variables from database and access values in blade

From Dev

Pandas: create new column from old, add values

From Dev

Create new list based on values taken from sublist

From Dev

Select values from dictionary to create a new DataFrame column

From Dev

Create new data frame column from past values

From Dev

Create new data frame based on values from another data frame

From Dev

How to create a new dataframe column with shifted values from another column?

From Dev

Get specifics values from column to create new lines

From Dev

How to create a new rows from column values of pandas data frame

Related Related

  1. 1

    Create a new column based on values from other variables

  2. 2

    Create new variables based upon specific values

  3. 3

    Create 2 new variables conditional on retaining values from previous rows using R

  4. 4

    How to create a new variable with values from different variables if another variable equals a set value in R?

  5. 5

    How to create a new variable with values from different variables if another variable equals a set value in R?

  6. 6

    jQuery: create variables from min and max values?

  7. 7

    Create new variables with lag data from all current variables

  8. 8

    Create a new color from two RGB values

  9. 9

    Create a new color from two RGB values

  10. 10

    Create new array with values from other array

  11. 11

    create new dataframe from missing values

  12. 12

    Get associative array values into variables,which is in string format from cookies

  13. 13

    Extracting multiple values from variable and assigning new variables

  14. 14

    Create a new list with values from fields from existing list

  15. 15

    From list create new one list with explicity values from first

  16. 16

    Make dummy variables from multiple columns and create new dataframe

  17. 17

    Create a new variable from existing variables efficiently in python

  18. 18

    Make dummy variables from multiple columns and create new dataframe

  19. 19

    How to create categorical variables from text values in R

  20. 20

    Create dynamic object from string, with variables as anchor property values

  21. 21

    Laravel: How to create variables from database and access values in blade

  22. 22

    Pandas: create new column from old, add values

  23. 23

    Create new list based on values taken from sublist

  24. 24

    Select values from dictionary to create a new DataFrame column

  25. 25

    Create new data frame column from past values

  26. 26

    Create new data frame based on values from another data frame

  27. 27

    How to create a new dataframe column with shifted values from another column?

  28. 28

    Get specifics values from column to create new lines

  29. 29

    How to create a new rows from column values of pandas data frame

HotTag

Archive