SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sir_Highbury
Quartz | Level 8

Dear expert,

 

after some checks I get a data set with the following structure:

 

data b;
input type: $12. variable: $12. variable_min;
datalines;
date begin 2000
num price 3000
date end 2500
;run;

 

How can I show for the same variable (variable_min) in some cases (when type='date') as date and in some cases (when type='num') as numeric?

 

Thanks in advance, SH.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have a character variable (only character can hol any value):

data b;
  length result $200;
  input type: $12. variable: $12. variable_min;
  select(type);
    when("date") result=put(variable_min,date9.);
    when("num") result=put(variable_min,best.);
    otherwise;
  end;
datalines;
date begin 2000
num price 3000
date end 2500
;
run;

Then put your number data in the correct text format.

View solution in original post

7 REPLIES 7
Sir_Highbury
Quartz | Level 8

Dear expert,

 

after some checks I get a data set with the following structure:

 

data b;
input type: $12. variable: $12. variable_min;
datalines;
date begin 2000
num price 3000
date end 2500
;run;

 

How can I show for the same variable (variable_min) in some cases (when type='date') as date and in some cases (when type='num') as numeric?

 

Thanks in advance, SH.

gamotte
Rhodochrosite | Level 12

It depends on what you mean by "show". If you mean display in the log then you can

precise the display format in the put statement :

 

data b;
	input type: $12. variable: $12. variable_min;

	if type="date" then do;
		put variable_min= date6.;
	end;
	else do;
		put variable_min;
	end;

	datalines;
date begin 2000
num price 3000
date end 2500
	;
run;
ballardw
Super User

Repeating the question without additional information is not very helpful.

Sir_Highbury
Quartz | Level 8

of course, sorry I did it by mistake.

ballardw
Super User

You would have to show the output you want for your example data? Do you want to create a new variable or are attempting to do the desired display in another procedure (unlikely to work).

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have a character variable (only character can hol any value):

data b;
  length result $200;
  input type: $12. variable: $12. variable_min;
  select(type);
    when("date") result=put(variable_min,date9.);
    when("num") result=put(variable_min,best.);
    otherwise;
  end;
datalines;
date begin 2000
num price 3000
date end 2500
;
run;

Then put your number data in the correct text format.

LinusH
Tourmaline | Level 20
I assume that want to use different SAS format depending on the type variable. This has a question here from time to time, and cannot be done by standard means.
As a storage pattern it's not a favourite of mine, even if some industry data models have this construct.
Either way, the easiest way is to use a CHAR data type.
Data never sleeps

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 7 replies
  • 1845 views
  • 1 like
  • 5 in conversation