SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Extract part of an observertion

Accepted Solution Solved
Reply
Super Contributor
Posts: 303
Accepted Solution

Extract part of an observertion

Hello , I wish to create a variable "M" that extract the observations marked "bold" from days

 

Obs

days

1

20150320

2

20140609

3

20140807

4

20140828

5

20150418

6

20150602

7

20150625

8

20141029

9

20150220

10

20150407

 

Thank you!!!


Accepted Solutions
Solution
‎06-27-2017 12:59 AM
Trusted Advisor
Posts: 1,137

Re: Extract part of an observertion

Posted in reply to desireatem

Hoping that the days variable is a character variable

 

data want;
set have;
m=substr(strip(days),5,2);
run;
Thanks,
Jag

View solution in original post


All Replies
Solution
‎06-27-2017 12:59 AM
Trusted Advisor
Posts: 1,137

Re: Extract part of an observertion

Posted in reply to desireatem

Hoping that the days variable is a character variable

 

data want;
set have;
m=substr(strip(days),5,2);
run;
Thanks,
Jag
Super Contributor
Posts: 303

Re: Extract part of an observertion

Posted in reply to Jagadishkatam

Thank you!!!!

Super User
Super User
Posts: 7,942

Re: Extract part of an observertion

Posted in reply to desireatem

Whilst @Jagadishkatam is quite correct, that with the example you have given in a string variable, you can substr out various parts, I would also point out two things.  Firstly that is a date field, and you would be best off treating it as a date.  Second as that is a string field you could have anything in it, I have seen:

YY0401 or 120405

The substr would not work in these cases, unless of course you do various checks before hand.  So when working with dates I always have the two components, the string value for display purposes to show it as entered, and a numeric version for processing.  You could for instance do:

mnth=month(input(days,anydtdte.));

Though why you would call a date variable days is beyond me.

Super Contributor
Posts: 303

Re: Extract part of an observertion

who cares what name I give to my variables. I just wanted to extract some number from it and some showed me, what is your issue with it.

Super Contributor
Posts: 303

Re: Extract part of an observertion

This is it, working well, what is your issue with trying to be coolest:

data have;
input obs days;
cards;

1 20150320
2 20140609
3 20140807
4 20140828
5 20150418
;
run;
data want;
set have;
m=substr(strip(days),5,2);
run;


proc print data=want;
run;
Obs obs days m
1 1 20150320 03
2 2 20140609 06
3 3 20140807 08
4 4 20140828 08
5 5 20150418 04


☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 235 views
  • 0 likes
  • 3 in conversation