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

Hi. 

I would like to record variable dynamically (I don’t know how many unique values) from:

Blue sky

white snow

etc

to

value1

value2

value3

etc.

 

thank you.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

A format is probably your best bet. Does the order matter? Do you need to keep consistency between the variables, ie value1 is always Blue Sky. 

 

*get list of unique values;
proc freq data=sashelp.cars noprint;
table origin / out=unique_origin;
run;

*create data set for proc format;
data origin_fmt;
set unique_origin;
start=origin;
label = catt("Value", put(_n_, 8. -l));
fmtname='origin_fmt';
type='C';
run;

*create fomrat;
proc format cntlin=origin_fmt;
run;

*apply format;
data remapped;
set sashelp.cars;
Origin_New = put(origin, $origin_fmt.);
run;

Similar ID to this I suppose:

https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644

 


@Zula wrote:

Hi. 

I would like to record variable dynamically (I don’t know how many unique values) from:

Blue sky

white snow

etc

to

value1

value2

value3

etc.

 

thank you.

 

 


 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
What's your question?
Reeza
Super User

A format is probably your best bet. Does the order matter? Do you need to keep consistency between the variables, ie value1 is always Blue Sky. 

 

*get list of unique values;
proc freq data=sashelp.cars noprint;
table origin / out=unique_origin;
run;

*create data set for proc format;
data origin_fmt;
set unique_origin;
start=origin;
label = catt("Value", put(_n_, 8. -l));
fmtname='origin_fmt';
type='C';
run;

*create fomrat;
proc format cntlin=origin_fmt;
run;

*apply format;
data remapped;
set sashelp.cars;
Origin_New = put(origin, $origin_fmt.);
run;

Similar ID to this I suppose:

https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644

 


@Zula wrote:

Hi. 

I would like to record variable dynamically (I don’t know how many unique values) from:

Blue sky

white snow

etc

to

value1

value2

value3

etc.

 

thank you.

 

 


 

Reeza
Super User
Note that I edited your question title as it was too generic. Please use a more descriptive title in the future.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 984 views
  • 1 like
  • 3 in conversation