DATA Step, Macro, Functions and more

show for the same variable as numeric or date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 127
Accepted Solution

show for the same variable as numeric or date

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.


Accepted Solutions
Solution
‎12-16-2016 03:23 AM
Super User
Super User
Posts: 7,970

Re: show for the same variable as numeric or date

Posted in reply to Sir_Highbury

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


All Replies
Frequent Contributor
Posts: 127

show for the same variable some observations as numeric and some as date

Posted in reply to Sir_Highbury

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.

Regular Contributor
Posts: 234

Re: show for the same variable some observations as numeric and some as date

Posted in reply to Sir_Highbury

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;
Super User
Posts: 11,343

Re: show for the same variable some observations as numeric and some as date

Posted in reply to Sir_Highbury

Repeating the question without additional information is not very helpful.

Frequent Contributor
Posts: 127

Re: show for the same variable some observations as numeric and some as date

of course, sorry I did it by mistake.

Super User
Posts: 11,343

Re: show for the same variable as numeric or date

Posted in reply to Sir_Highbury

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).

 

 

Solution
‎12-16-2016 03:23 AM
Super User
Super User
Posts: 7,970

Re: show for the same variable as numeric or date

Posted in reply to Sir_Highbury

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.

Super User
Posts: 5,431

Re: show for the same variable as numeric or date

Posted in reply to Sir_Highbury
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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 192 views
  • 1 like
  • 5 in conversation