BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
I have a dataset with multiple columns with name starting ind_ (eg ind_name, ind_adr,ind_sal) and the values are either 1 or 0 . I will need to convert those values to Y or N with the variable names keeping same.

I tried using proc format and the columns showed correct values of Y or N but they were numeric.

Is there a way I can achieve that by also changing the data type to character?

Thanks in advance
6 REPLIES 6
SASKiwi
PROC Star

You can't change the type of an existing SAS variable, you have to create a new variable based on the old one. One common trick is to rename the variable you want to switch like below, then recreate the original variable:

proc format;
  value yn
  0='N'
  1='Y';
run;

data want;
  drop tmp_:;
  set have (rename = (MyYesNoVar = tmp_MyYesNoVar));
  MyYesNoVar = put(tmp_MyYesNoVar, yn.);
run;
nickspencer
Obsidian | Level 7
@SASKiwi I have multiple variables to change. Is there a way to achieve this at a time for multiple variables ?
SASKiwi
PROC Star

@nickspencer  - Yes. An array in the example code I posted could do this. How many variables do you want to change?

data_null__
Jade | Level 19

This will convert all 0/1 to n/y.  Many variables. And preserve variable order as specified in VAR statement.

 

 

data ind;
   length ind_name ind_adr ind_sal 8.;
   array _ind[*] ind_:;
   do subject=1 to 10;
      do i = 1 to dim(_ind);
         _ind[i] = ranbin(1234,1,.5);
         end;
      output;
      end;
   drop i;
   attrib ind_: label='Label to preserve';
   run;
proc format; value yn 1='Y' 0='N';
proc transpose data=ind out=flip;
   by subject;
   var ind_:;
   format ind_: yn.;
   run;
proc print;
   run;
proc transpose data=yn out=ind2(drop=_name_);
   by subject;
   var yn;
   id _name_;
   idlabel _label_;
   run;
proc print;
   run;
proc contents varnum;
   run;

 

image.pngimage.png

nickspencer
Obsidian | Level 7
@SASKiwi there are 15 variables but they all start with the name ind_
SASKiwi
PROC Star

Here is the start of a solution. It could be improved by dynamically creating a RENAME = list using SAS DICTIONARY tables.

data want;
  drop tmp_:;
  array indicators (*) ind_V1 ind_V2;
  array tmp_indicators (*) tmp_ind_V1 tmp_ind_V2;
  set have (rename = (ind_V1 = tmp_ind_V1 ind_V2 = tmp_ind_V2));
  do i = 1 to dim(indicators);
   indicators(i) = put(tmp_indicators(i), yn.);
  end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 3606 views
  • 0 likes
  • 3 in conversation