08-05-2015 11:55 AM
You question if too vague. While you make it more clear in your follow-up post consider this data step and how it might be used.
08-05-2015 01:26 PM
hi data _null_ - sorry for the vague question. I have a categorical field month in SAS Visual analytics dataset. I was trying to convert to a numeric field so that I could use mdy function. I see your code but how do I use it in SAS VA 7.1?
08-05-2015 01:41 PM
Try creating a calculated item. In the operators section look for date. There should be one that says month. That will return the number of the month. Don't forget, if your data is coming in with a time, you will need to use the DatePart operator as well.
08-11-2015 10:14 AM
Thanks MIke, wouldnt this return an entire date (or alternatively a sas number that stands for a date)? I was looking to convert a month series Jan - Jul to a number series 1-6.
08-05-2015 05:00 PM
A trick I've used...
input month_txt :$16.;
month_num = month(input(cats('01', substr(month_txt, 1, 3), '2000'), date9.));
08-11-2015 03:04 AM
You can't show character to numeric format...............
In SAS VA
IF ( 'Month'n In ('Jan') )
IF ( 'Month'n In ('Feb') )
IF ( 'Month'n In ('Mar') )
IF ( 'Month'n In ('Apr') )
IF ( 'Month'n In ('May') )
IF ( 'Month'n In ('Jun') )
IF ( 'Month'n In ('Jul') )
IF ( 'Month'n In ('Aug') )
IF ( 'Month'n In ('Sep') )
IF ( 'Month'n In ('Oct') )
IF ( 'Month'n In ('Nov') )
IF ( 'Month'n In ('Dec') )
ELSE . ) ) ) ) ) ) ) ) ) ) )
You can achieve your requirement by doing above.
Thanks & Regards,
08-11-2015 03:57 AM
This is not good advice. Firstly the question has been answered quite simply by either data _null_'s code, or SAS_Mike's response for VA specific. Secondly, if you need to multiple if statements like that then use select-when syntax:
when('January') then 1;
Thirdly, even if for some reason you need to use if statements, then the nesting is not necessary, you can use else if, the syntax of which is:
if <condition> then <result>;
else if <condition> then <result>
if month="January" then 1;
else if month="Febuary" then 2;
Finally it would be a good idea to look up good coding practice, e.g: lower casing code - other than SQL it just makes it hard to read. Consistent indetation. Align brackets so the set of statements can be clearly identified.
08-11-2015 05:56 AM
I agree with you. but he wants to calculate in SAS VA. data_null_'s answer is about back end (SAS EG) data calculation and Mike's Answer is suitable when the columns are in date format.
In front end (SAS VA) we are not able to change Character to Numeric (we are able to change Date format to numeric format), in this case we have only one alternative i.e.., if else statement to achieve his requirement.
Thanks & Regards,
08-11-2015 10:09 AM
I would agree with Venkata. The coding is fine when you are working in SAS EG. But in SAS VA 7.1, there is no code editor that lets me build a format/data step/select statement.
08-11-2015 10:12 AM
I initially tried the nested if function. I am surprised you got it to work. I guess I had to have more patience in order for the nesting conditions to work. Only the first if then condition would work for me. I experimented quite a bit for the conversion of months from Jan - Jul to number series 1 - 6.
08-11-2015 04:26 AM
Formats are an option:
value $ monnum 'January' = '1' 'February' = '2' ...
*and in the datastep;
month_num = put(month, $monnum.) + 0;
(some programmers don't like those automatic conversions :-))
08-11-2015 02:36 PM
Its not pretty but the following will work in VA. I would prefer to do these type of calculations on the backend, much simpler.
Month(Parse(Concatenate(Concatenate('01', Substring('MyMonth'n, 1, 3)), '2099'), 'DATE7.'))
08-11-2015 03:34 PM
Thanks - I was looking for somehting like this. I actually did a very crude conversion in SAS VA. There is a facility to name a new custom category and then use a choice of labels. So what I did was add 6 labels 1-6 and then assign the months Jan - Jul for that. I have to see if that will create further unexpected issues in which case I will try your formula. After creating this custom category, I created another calculated item which took this custom category as input for mdy function. For the year argument, I used 2015. For the date, I had a field from the sas dataset.