BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 10

 

Proc freq data=have;

Var listofallchar;

Run;

 

 data want;

set have;

if chav1="?" then chav1="15.0";

if chav2="?" then chav2="1.0";

 ... (25 char variables to type with 25 mode values)

 

 

 

this is the original code but I am tried to type all these char variables with their values. Any advice to use macro or a library? Thank you

4 REPLIES 4
Reeza
Super User

@Bal23 wrote:

 

 

this is the original code but I am tried to type all these char variables with their values. Any advice to use macro or a library? Thank you


Try an array instead.

 

https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

If you have trouble, post your code, log and detailed explanation of the issue.

Bal23
Lapis Lazuli | Level 10

Proc contents data=have;

Run;

proc means data=have n missing;

var _numeric_;

run;

 

*there is no missing value for all numerical variables;

 

proc freq data=have;

tables listofallcharvariables;

run;

*by copy and paste the result to an excel file, sort it by the frequency of the value, descending (that is how I get each value that most people have for each variable; actually I notice all variables could be considered as numerical, the only thing is, it has “?” when the data is missing, so when there is “?”, this variable is considered as “char”. Could we convert all character variables to numerical variables and replace “?” to “.”)

 

data want;

 

set have;

 

if chav1="?" then chav1="15.0";  /*15.0 is the value that most people have for this variable, same rule applies below/

 

if chav2="?" then chav2="1.0";

 

 ... (25 char variables to type with 25 mode values)    /*when i tried to type these 25 variables, it takes so much time, that is why I am seeking help*/

run;

Reeza
Super User

Look at PROC STDIZE or STANDARDIZE (can't recall which) which have replacement for missing values options. Not sure Mode is an option but most other common metrics are such as median/mean or other similar measures.

 

Nothing from your response below negates my suggestion of using a loop to test each value and replace it.


@Bal23 wrote:

Proc contents data=have;

Run;

proc means data=have n missing;

var _numeric_;

run;

 

*there is no missing value for all numerical variables;

 

proc freq data=have;

tables listofallcharvariables;

run;

*by copy and paste the result to an excel file, sort it by the frequency of the value, descending (that is how I get each value that most people have for each variable; actually I notice all variables could be considered as numerical, the only thing is, it has “?” when the data is missing, so when there is “?”, this variable is considered as “char”. Could we convert all character variables to numerical variables and replace “?” to “.”)

 

data want;

 

set have;

 

if chav1="?" then chav1="15.0";  /*15.0 is the value that most people have for this variable, same rule applies below/

 

if chav2="?" then chav2="1.0";

 

 ... (25 char variables to type with 25 mode values)    /*when i tried to type these 25 variables, it takes so much time, that is why I am seeking help*/

run;


 

s_manoj
Quartz | Level 8

I think , following might reach your need

data want;
set have;
array chr [*] $ _character_;
do i = 1 to dim(chr);
if cmiss(of chr[*]) then /*write your condition here*/;
output;
end;
output; run;

Note: I haven't tested this code; so, am not sure about its performance. 

Since am not very clear about your data, so why I haven't mentioned condition after "then"; If mode values were provided already then create macro for each value and give those macros as the reference after "then ".

MODE is a stat which you can be generated for a numeric variable.

 

Thank you & Regards,

Manoj.

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
  • 4 replies
  • 3725 views
  • 0 likes
  • 3 in conversation