BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have synamic data set .

It means that in different situations (depends on user macro values) there are different variables in the data set.

I would like to convert each variable value to its formatted value.

I saw the way to do it via vvalue function.

X_=vvalue(X);

My question:

What is the way to perform this conversion to all variables in the data set?

I am looking for dynamic code that will do it for each set of variables(because in different situations will have different varaibles)

I also want that the converted variables will have same name as the orginal variables.

proc format;
value X_Fmt
1='A'
2='B'
;
value Y_Fmt
1='Y'
2='N'
;
Run;

data have;
format x X_Fmt.  y Y_Fmt.;
input ID  X Y W Z;
cards;
1 1 1 1 2
2 1 2 2 2
3 1 1 3 2
4 2 2 3 2
5 2 1 2 1
;
Run;


data want(Drop=X Y W Z rename=(X_=X  Y_=Y  W_=W  Z_=Z));
set have;
X_=vvalue(X);
Y_=vvalue(Y);
W_=vvalue(W);
Z_=vvalue(Z);
Run;

 

2 REPLIES 2
Tom
Super User Tom
Super User

Use code generation.

Get the list of variable names. For example using PROC TRANSPOSE.

proc transpose data=have(obs=0) out=names ; 
  var _all_;
run;

Use the names to generate the code:

data _null_;
  set names end=eof;
  varnum+1;
  length nliteral $60 ;
  nliteral=nliteral(_name_);
  if _n_=1 then call execute('data want; set have;');
  call execute(catx(' ',cats('__',varnum),'=vvalue(',nliteral,');'
      ,'drop',nliteral,';rename',cats('__',varnum),'=',nliteral,';'
  ));
  if eof then call execute('run;');
run;

Results in generated code like this:

1   + data want; set have;
2   + __1 =vvalue( Name ); drop Name ;rename __1 = Name ;
3   + __2 =vvalue( Sex ); drop Sex ;rename __2 = Sex ;
4   + __3 =vvalue( Age ); drop Age ;rename __3 = Age ;
5   + __4 =vvalue( Height ); drop Height ;rename __4 = Height ;
6   + __5 =vvalue( Weight ); drop Weight ;rename __5 = Weight ;
7   + run;

Getting a reasonable LENGTH for the new character variables is a harder problem.

 Variables in Creation Order

#    Variable    Type    Len

1    Name        Char    200
2    Sex         Char    200
3    Age         Char    200
4    Height      Char    200
5    Weight      Char    200

You might want to do that AFTER converting them, unless some of them need lengths longer than 200.

 

 

 

ballardw
Super User

Why?

 

SAS will use the formatted values for analysis, reporting or graphing. So this does not really make much sense.

 

Export the data to a text file using the formatted values. 

Read them back in. Though this step is a waste of time as SAS uses the formats for any purpose that makes sense.

The Export step creates enough code that it is easy to rename the variables (if that is really important).

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 887 views
  • 0 likes
  • 3 in conversation